I am a newbie in javascript. I am not sure what am I doing wrong here. I want to pass in a variable which is a function to an onclick event of an anchor tag.I could not figure out. Thanks in advance.
var test = function(val1, val2) { alert("bla"); };
$.each( result, function (key, value){ var a = key; var b = key+value; var doSomething = function() { test (a, b)); $('#id').bind("click", test); });
I need to create the anchor tags dynamically and pass multiple variables to "onclick" event. so what I am trying to do is create a variable function dynamically inside a for loop and bind it to an anchor tag
try this code:
var test = function() {
alert("hello");
};
$("#myUL").append("<li><a href='#' onclick=test()>test</a></li>");
Is what you want?
You need something like this:
for (var i = 0; i < 5; i++) {
$("button").eq(i).on("click", {value: i}, function(event) {
var msgs = [
"button = " + $(this).index(),
"event.data.value = " + event.data.value,
"i = " + i
];
logDiv.append( msgs.join(", ") + "<br>" );
});
}
jQuery site: event.data
You should use :
$(document).ready(function(){
$("#myUL").click(function(){
alert("hello");
});
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.