Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call function on div click

I am doing this: <div onclick='alert("xxx")'>Click me!</div> and i see that alert but i want to call on function inside that onclick. I'm trying this but it doesn't work.

  function GetContent(prm){
    alert(prm);
  }

  <div onclick='GetContent("xxx")'>Click me!</div>

I need to call that function inline not assign an id or class to that div and use jquery. What is the solution? Thanks

like image 833
luke Avatar asked Mar 11 '10 10:03

luke


2 Answers

Using code inline is bad practice, you need to assign an ID or Class to the div and call function against it eg:

<div class="test">Click me!</div>

$('div.test').click(function(){
    GetContent("xxx");
});

.

I need to call that function inline not assign an id or class to that div and use jquery.

I think you are already doing that with this code:

<div onclick='GetContent("xxx")'>Click me!</div>

Calling function inline without assigning id or class. But as said before, it is not good practice to use inline code.

like image 184
Sarfraz Avatar answered Oct 10 '22 05:10

Sarfraz


You can give that div a class and then do something like this

$("div.myclass").click(function(){
    GetContent("xxx");
});

<div class="myclass"></div>

This will fire click event to all div elements with class 'myclass'.

But I am not sure why you don't want to give an id to the div element.

like image 33
rahul Avatar answered Oct 10 '22 05:10

rahul