Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have a jQuery function that generates input text on click of some add button.
generated code looks like:

<input type="text" id="txtNum" name="some model binding" class="FireEvent" />


there is a jquery function:
$(".FireEvent").change(
alert("called");
);

all this code is in .Ready() function still on text change this event is not triggered.

i have also tried $(".FireEvent").bind("input",function(){}); this is also not working.

However if i try to fire this event from controls that are in my view it works perfect.
i.e.
@Html.TextBox("name", "", new { @class = "FireEvent", @id = "someId" });
Or
<input type="text" id="someId" name="some binding" class="FireEvent" />
Posted
Updated 27-May-15 5:43am
v2

Either attach your events to the new elements when you create them, or use the "on" method of binding an event to a selector

http://api.jquery.com/on/[^]

"on" works for elements created after the code is first run, but note it has a performance penalty.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-May-15 12:25pm    
Sure, a 5.
—SA
Try using $(document).on('click','.FireEvent',function(){.......});

You can find a wonderful explanation here : http://stackoverflow.com/a/14879213[^]

Regards..
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-May-15 12:25pm    
Sure, a 5.
—SA
Thanks7872 28-May-15 1:44am    
Thanks Sergey.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900