I need a way to solve this problem. live()
is removed in the jQuery 1.9 which I was using. so now I don't know how to do add an element into the document object model tree dynamically.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Trying dynamic adding</title>
<style>
body {
margin:10%;
}
#cool {
border:5px solid red;
}
.fool {
background-color:red;
width:100px;
height:100px;
border:1px solid green;
}
.lol {
background-color:yellow;
}
</style>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
alert("Application has been started");
$('#hool').addClass("lol");
$('#create').click(function(){
alert("Element created here");
var a=$('#cool');
b="<div class='fool'></div>";
a.append(b);
alert("Element created");
var c=$('.fool');
c.addClass("lol");
});
$('.lol').click(function(){
alert("New elements are good to go");
$('.lol').css("background-color","teal");
});
function hello(){
alert("welcome");
}
});
function hello(){
alert("welcome");
}
</script>
</head>
<body>
<section id=cool>
<button id=create> Create </button>
<button id=hool>Doubt</button>
</section>
</body>
</html>
In this code the problem is when I am clicking the button with id create it is creating new division tag with the class fool
. which is only working with the css and not with the javascript. for example button with id hool
and new division tags created with class fool are getting assigned to another class called lol
. Now the button is working when i click button $('.lol').click(function)
is working. but when i press the newly created tags it is not executing the function. because the button was added when the page was loaded but the new div tags are created using append method dynamically.
Now please someone tell me how to run the $('.lol').click(function)
by clicking the new div tag. I am currently doing one project so please help.
All the answers are welcomed. Thanks in advance.