Click here to Skip to main content
15,893,904 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Folks,

How to assign events in jquery for the dynamically adding html controls.

For example:

I have added some control in DOM ready.
$(document).ready(function(){
...
     $(obj).each(function(){
          str += "<input id='txt" + ID + "' type='text' title='" + Text + "' /></li>" //Here adding the control
          ...
          //Here I want to add some event
          $("#txt" + ID).change(function(){
             ...
             ...
             ...
          });
     });
});


If I debug I am not getting the object for the statement $("#txt" + ID)

Help me where I am missing.

Thanks.
Posted

Strange question, because this is the most fundamental approach in jQuery. If you look at your own code, you will see that you are already adding a handler of some event dynamically, change event, for example.

The problem is your HTML. Your resulting HTML containing id is incorrect. For example, if ID is "SomeInput", your code will yield:

<input id='txtSomeInput'><!-- ... --></input>
but it should be
<input id="txtSomeInput"><!-- ... --></input>
This is just the first bug I could spot.

—SA
 
Share this answer
 
Comments
Kiran B S 1-Oct-13 3:36am    
For readability I have given like this actually I used double quotes like \" instead '.
Moreover, giving single quotes is working fine for other statements.

Here I wanted is before response to the browser need to add the event, I guess it is not possible but give me other way to achieve this, i.e., Add event for the dynamic control.
Sergey Alexandrovich Kryukov 1-Oct-13 8:45am    
There is one more possibility: you did it all correctly but fail to see it. The change event on text input is confusing: it is not invoked when you enter characters in it. Your function will only be invoked when you shift focus from a changed input. Try to enter a character and then press Tab. For debugging, add something obvious to the handler, like "alert('working')".

Another possible problem is: are you changing the value of ID on each iteration? All the values of the id parameters on the whole page should be unique. If they are not, the behavior is unpredictable.

If you do it all correctly and it does not work, the problem could be somewhere else, in your '...' fragments. You can reduce the code sample to absolute minimum and show the whole code.

—SA
Really I felt shy on the way I did coding.

Actually the issue is:
I was binding the event before adding it in to a html tag

I solved by this.

Created a variable, and in the loop assigned the value with comma separated and prefixed with # symbol on each value.

After appending the elements in a div tag then bind the "change" event using the variable.

$(document).ready(function(){
...
     var InputIds = ""; //Variable to capture ids.
     $(obj).each(function(){
          str += "<li><input id="txt" + ID + "" type="text" title="" + Text + "" /></li>"
          InputIds += "#" + "txt" + ID + ", "; //Assigning values with comma separation and prefixed the # symbol.
          ...
     });
     $("#DivInputFields").append(str); //Adding the input fields
     //Remove the end comma from the variable InputIds
     $(InputIds).change(function(){
         ...
         ...
         ...
     });

});


I don't know whether this is right or wrong but working. If am doing wrong please help me.

Thank you Sergey Alexandrovich, I understand the problem in question posted here, I will improve myself to post with clear code.

-Kiran. B. S
 
Share this answer
 

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