Reputation: 45
I added onclick event to below query, but it doesn't work. I used ruby on rails . Help me out to get a solution. Thank you...
for (var j = 0; j < 3; j++){
$('#sub').append("<div class='col-md-12' '><img src='picture.jpg' class='subPic' style='cursor:pointer;' onclick='sayhello()'></div>");
}
function sayhello(){
alert("hello");
}
Upvotes: 1
Views: 73
Reputation: 2189
Your function needs a name. I chose the name "callAlert" here.
for (var j = 0; j < result["prd_image"].length; j++){
$('#sub').append("<div class='col-md-12' '><img src='picture.jpg' class='subPic' style='cursor:pointer;' onclick='callAlert()'></div>");
}
function callAlert(){
alert("hello");
}
Upvotes: 1
Reputation: 27041
You need to assign a name to the function, like function sayhello()
and then call it like onclick='sayhello()'
for (var j = 0; j < 2; j++) {
$('#sub').append("<div class='col-md-12' '><img src='picture.jpg' class='subPic' style='cursor:pointer;' onclick='sayhello()' alt='awda'></div>");
}
function sayhello() {
alert("hello");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="sub"></div>
Upvotes: 1