Access through Javascript/Jquery an element that came through AJAX after DOM LOAD

Asked

Viewed 435 times

5

I am developing a WEB APP with Angularjs, there is an element that is coming to the DOM after its loading (load), I am trying to write an event :

$('elemento').click()

But when finished rendering the page the element still does not exist as it is coming by ajax through Angular, and because of this the event is not being associated with it.

How to associate Events or Capture elements that enter the DOM after your LOAD?

  • Via Angular, use $compile(elemento) for him to be interpreted.

1 answer

1

I solved my problem! I needed to make an element that came dynamically through AJAX be recognized through Javascript/Jquery and be able to write an Event for this element.

Be able to do this through the . live() Jquery method, follow the example:

$('elemento').live('click',function(){})
  • Do you use Jquery 1.4? It would be nice to have asked the question.

  • 3

    The ideal is to use the .on() as follows: $('elementoPai').on('click', 'elemento', function(){});, since live has been deprecated in version 1.7 and removed in version 1.9. If you need to update jQuery in the future, your code will no longer work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.