2

I have some jQuery functions defined over some mouse events, for example something like

$(".divexpand").mouseenter(function(){
var alto = $(this).find('.checkalign').length*21+"px";
$(this).animate({height: alto}, 100)
$(this).css("z-index","100")
});

This works well in all my .divexpand classes, the problem is when i have some of my content generated with ajax, something like

ajax.open("GET", "modules/activityedit-prop.php?id="+id);
ajax.onreadystatechange=function() {
if (ajax.readyState==4) {
 divContenido.innerHTML = ajax.responseText
 }
}

In that case, .divexpand classes in activityedit-prop.php are ignoring the jQuery functions...

Can someone tell me why?

Thanks

Zakaria Acharki
67.5k15 gold badges79 silver badges106 bronze badges
asked Dec 1, 2015 at 18:17
2
  • You have to add a listener on divContenido Commented Dec 1, 2015 at 18:22
  • Sorry Hackerman, could you explain that a little more? I don't know what it is :( Commented Dec 1, 2015 at 18:27

2 Answers 2

3

Replace :

$(".divexpand").mouseenter(function(){

By :

$("body").on('hover', '.divexpand', function(){

You have to use on() to deal with tags added dynamically to the page.

Hope this helps.

answered Dec 1, 2015 at 18:20
Sign up to request clarification or add additional context in comments.

5 Comments

Make sure that the new divs has class divexpand.
I didn't saw your edit! Now is ok, is the same response as spaniol6, thank you at all :D
@SLaks please recheck my answer.
Adding a listener to the entire document vs just the body....your code seems better to me... +1
Thanks @Hackerman I wasn't aware using document was bad practice. I updated my answer and upvoted this one.
2

This is because when you run $(".divexpand").mouseenter() it binds a mouseenter event to each divexpand that exists on the DOM at that time. So by the time your AJAX is ran, the binding has already happened.

What you need to do is bind the mouseenter event to an element that encompasses all .divexpand (such as the body) and tell it to trigger only on child elements that fit a certain selector. more about jQuery.on

$("body").on('mouseenter','.divexpand',function(){
 var alto = $(this).find('.checkalign').length*21+"px";
 $(this).animate({height: alto}, 100)
 $(this).css("z-index","100")
});

Edit:

After reading Hackerman's comment on Zakaria Acharki's answer I looked up if there was really anything wrong with using document as the delegate. I found this answer on a separate question that explains that you shouldn't use document to delegate events and it's best practice to use the closest parent to the element. I updated my code to use body instead of document (since i don't know what the closest parent element in this case is).

answered Dec 1, 2015 at 18:26

1 Comment

@EstebanCazorla I made an update based on some comments on the other answer. Please make sure to use the closest parent element to all the .divexpand elements instead of document.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.