Note
Example: 1 .on() function used for,
If you create some element dynamically in page, for example by append() function.
Click function will not work for dynamically created element.For that,use:
Related Topics:
Example: 2 .bind() function
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | < html >
< head >
< title >HTML-JQuery</ title >
< script type = "text/javascript" >
$(function () {
$(".container").click(function () {
$(".new").append('< div class = "new" >new Element</ div >');
});
$(".new").on("click", function () {
$(this).css("color", "blue");
});
});
</ script >
</ head >
< body >
< div class = "container" >click here to append element</ div >
< div class = "new" >new Element</ div >
</ body >
</ html >
|
|
Example: 1 Demo
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | < html >
< head >
< title >HTML-JQuery</ title >
< script type = "text/javascript" >
$(function () {
$(".container").click(function () {
$(".new").append('< div class = "new" >new Element</ div >');
});
$(".new").unbind("click");
$(".new").bind("click", function () {
$(this).css("color", "blue");
});
});
</ script >
</ head >
< body >
< div class = "container" >click here to append element</ div >
< div class = "new" >new Element</ div >
</ body >
</ html >
|
|