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 wor开发者_JAVA百科k.
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?
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.
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.
精彩评论