admin管理员组文章数量:1023078
I try to add some list element in loop via JS. Every element <li>
contains <a>
tag, now I want to add onClick event in every adding <a>
tag. I try to do it so:
liCode = '<li><a href="#">Text using variable foo: ' + foo + '</a></li>';
$('#list').append(function() {
return $(liCode).on('click', clickEventOccurs(foo));
});
In clickEventOccurs
I just output to console foo. It works in strange way: this event performed just on init when every tag is adding to list, but after click on <a>
doesn`t perform anything. How to make it works in proper way - on click performed code in clickEventOccurs?
I try to add some list element in loop via JS. Every element <li>
contains <a>
tag, now I want to add onClick event in every adding <a>
tag. I try to do it so:
liCode = '<li><a href="#">Text using variable foo: ' + foo + '</a></li>';
$('#list').append(function() {
return $(liCode).on('click', clickEventOccurs(foo));
});
In clickEventOccurs
I just output to console foo. It works in strange way: this event performed just on init when every tag is adding to list, but after click on <a>
doesn`t perform anything. How to make it works in proper way - on click performed code in clickEventOccurs?
-
You can add
onclick="your_function()"
to the<a>
tag in the string. – MahanGM Commented Jul 29, 2013 at 9:33 - but I can solve problem how to add to the tag onClick="my_function(foo)", where foo is the variable in JS. It showing me error in parsing. – sphinks Commented Jul 29, 2013 at 10:53
4 Answers
Reset to default 4Firstly, you are assigning not a callback function, but a result of function evaluation. In right way it should be like this:
$('#list').append(function() {
return $(liCode).click(function() {
clickEventOccurs(foo);
});
});
Also, as you are using jQuery you might use benefits of events delegation and use .on
method this way:
$('#list').on('click', 'li', function() {
return clickEventOccurs(foo);
});
on()
is good for handling events, even to elements which will be created dynamically.
$('body').on('click', '#list li', function(){
clickEventOccurs(foo);
});
http://jsfiddle/lnplnp/uGJnc/
HTML :
<ol id="list">
<li><a href="#">Text using variable foo: foovalue</a></li>
</ol>
JAVASCRIPT/JQUERY :
function appending(foo) {
liCode = '<li><a href="#">Text using variable foo: ' + foo + '</a></li>';
$('#list').append($(liCode));
}
$('#list').on('click', 'li', function() {
return clickEventOccurs($("a", this).text());
});
function clickEventOccurs(v){
console.log(v.split(":")[1].trim());
}
appending("foo1");
appending("foo2");
appending("foo3");
To pass a variable to that function you'll have to make a second anonymous one, otherwise your clickEventOccurs function will be called at assignment, not as a callback.
$('#list').append(function() {
return $(liCode).click(function() {
clickEventOccurs(foo)
});
});
I try to add some list element in loop via JS. Every element <li>
contains <a>
tag, now I want to add onClick event in every adding <a>
tag. I try to do it so:
liCode = '<li><a href="#">Text using variable foo: ' + foo + '</a></li>';
$('#list').append(function() {
return $(liCode).on('click', clickEventOccurs(foo));
});
In clickEventOccurs
I just output to console foo. It works in strange way: this event performed just on init when every tag is adding to list, but after click on <a>
doesn`t perform anything. How to make it works in proper way - on click performed code in clickEventOccurs?
I try to add some list element in loop via JS. Every element <li>
contains <a>
tag, now I want to add onClick event in every adding <a>
tag. I try to do it so:
liCode = '<li><a href="#">Text using variable foo: ' + foo + '</a></li>';
$('#list').append(function() {
return $(liCode).on('click', clickEventOccurs(foo));
});
In clickEventOccurs
I just output to console foo. It works in strange way: this event performed just on init when every tag is adding to list, but after click on <a>
doesn`t perform anything. How to make it works in proper way - on click performed code in clickEventOccurs?
-
You can add
onclick="your_function()"
to the<a>
tag in the string. – MahanGM Commented Jul 29, 2013 at 9:33 - but I can solve problem how to add to the tag onClick="my_function(foo)", where foo is the variable in JS. It showing me error in parsing. – sphinks Commented Jul 29, 2013 at 10:53
4 Answers
Reset to default 4Firstly, you are assigning not a callback function, but a result of function evaluation. In right way it should be like this:
$('#list').append(function() {
return $(liCode).click(function() {
clickEventOccurs(foo);
});
});
Also, as you are using jQuery you might use benefits of events delegation and use .on
method this way:
$('#list').on('click', 'li', function() {
return clickEventOccurs(foo);
});
on()
is good for handling events, even to elements which will be created dynamically.
$('body').on('click', '#list li', function(){
clickEventOccurs(foo);
});
http://jsfiddle/lnplnp/uGJnc/
HTML :
<ol id="list">
<li><a href="#">Text using variable foo: foovalue</a></li>
</ol>
JAVASCRIPT/JQUERY :
function appending(foo) {
liCode = '<li><a href="#">Text using variable foo: ' + foo + '</a></li>';
$('#list').append($(liCode));
}
$('#list').on('click', 'li', function() {
return clickEventOccurs($("a", this).text());
});
function clickEventOccurs(v){
console.log(v.split(":")[1].trim());
}
appending("foo1");
appending("foo2");
appending("foo3");
To pass a variable to that function you'll have to make a second anonymous one, otherwise your clickEventOccurs function will be called at assignment, not as a callback.
$('#list').append(function() {
return $(liCode).click(function() {
clickEventOccurs(foo)
});
});
本文标签: How to add onclick event in dynamicly add html code via JavaScriptStack Overflow
版权声明:本文标题:How to add onclick event in dynamicly add html code via JavaScript? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745581931a2157361.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论