Note
Example: 1 .bind() 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 .on() function
|
<html>
<head>
<title>HTML-JQuery</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<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>
|
Example:1 Demo
|
<html>
<head>
<title>HTML-JQuery</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<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: 2 Demo
|