0

I am trying to dynamically add javascript for new elements I obtain & add to the page through ajax. So I can dynamically add javascript using the createElement("script") and appendChild for elements already on the page, but for new elements I just added through ajax, that doesn't seem to work. Any suggestions how I can achieve this?

btw I am adding new DOM elements using .innerHTML += instead of appendchild for each, since there is quite a lot of new elements. Does that matter?

Thanks.

4
  • What do you mean with "add javascript for new elements"? Commented Sep 1, 2012 at 21:47
  • I believe that innerHTML and appendchild serve entirely different purposes. You should be careful in your use of either. You might also find it useful to play around with style.display and style.visibilty attributes. Commented Sep 1, 2012 at 21:50
  • I mean add js for handling the new html elements that were added from the ajax response Commented Sep 1, 2012 at 23:21
  • it's quite a lot of stuff so I don't really want to use style.display to hide and then turn on. Commented Sep 1, 2012 at 23:21

3 Answers 3

1

Best to employ the help of a framework like jQuery.

With the help of jQuery, the work of tracking and working with dynamically added DOM elements becomes trivial:

$.(document).on("click","#MyElement",function(){ //do stuff });

Good luck!

Sign up to request clarification or add additional context in comments.

2 Comments

ugh yes, i totally forgot about .live
@user1181950 Cool. If it helped, please vote up and accept the answer. Thanks and let me know if I can be of further help!
0

Instead of dynamically adding javascript, you should set aside dedicated functions in your <script> section in the header or as a separately sourced file. Once your elements are added, you should add event handlers that call these dedicated functions without needing to add javascript dynamically.

3 Comments

ok, if that is the right way to do it, then i can do that easily. But just so I know, is there a way to do the above at all? thanks!
So I actually tried what you suggested. Have the js already loaded in page, and then adding only the html from ajax. Does it matter i am using jQuery for the javascript as well?
@user1181950 I'm not so knowledgeable on jQuery, but I believe you should be able to do so. As for adding javascript dynamically, I am not certain. Maybe you can try looking into this
0

suppose you have loaded your contents to a container called with id="container" you might wanna use a selector like

$("#container img")

to get the elements and set the event handlers.

here is an example: http://jsfiddle.net/PTcw8/5/

1 Comment

yea as mentioned below, i totally forgot about .live in jquery ><

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.