Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
131
rated 0 times [  132] [ 1]  / answers: 1 / hits: 15898  / 10 Years ago, tue, september 2, 2014, 12:00:00

I have a problem with .click() event in jquery. I create an input tag with a button, and then when I click this tag it must show an alert message. But it doesn't work.
Here is the HTML code:



<div id=test>

</div>
<input type=button id=btn value=Submit />


And the Jquery code:



$(#btn).click(function(){    
html = <input type='text' size='1' name='courses[]' value='1' />;
$(#test).after(html);
});
$(input[type='text']).on(click, function(){
alert('ppp');

});


All libraries of jquery are linked in the real site.
Here is an example in jsFiddle: http://jsfiddle.net/82pps6Lz/29/
Thanks


More From » jquery

 Answers
25

Use this:


$(document).on("click","input[type='text']", function(){             
alert('ppp');
});

The above is how you use event-delegation. You can also bind after <input> exists.


DEMO


[#69585] Friday, August 29, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jenamackennac

Total Points: 304
Total Questions: 110
Total Answers: 107

Location: Ecuador
Member since Thu, Jun 4, 2020
4 Years ago
jenamackennac questions
Fri, Feb 18, 22, 00:00, 2 Years ago
Wed, Apr 21, 21, 00:00, 3 Years ago
Thu, Apr 1, 21, 00:00, 3 Years ago
Tue, Feb 2, 21, 00:00, 3 Years ago
;