0

I am having some problems getting this part of my jQuery script to run after the Ajax – content switch...

/*no right click on img*/
 $('img').bind('contextmenu', function(e) {
 return false;
/*rollover*/
 $('.roll').on('mouseenter mouseout', function() {
 var original = $(this).attr('src');
 var replacement = $(this).data('hoverimg');
 $(this).attr('src', replacement).data('hoverimg', original);
 });
 });

Could someone please help me get it running I tried it whit a lot of Ideas, like:

var init = function(){
/*Code*/
}

Or

function init(){
/*Code*/
}

I am a bit confused right now.

Cœur
39k25 gold badges207 silver badges282 bronze badges
asked Oct 4, 2013 at 9:14
6
  • Is there any error that you get ? Commented Oct 4, 2013 at 9:16
  • Where's the ajax code? Also, your indenting is a little weird, but the code shown appears to assign the mouseenter and mouseout handlers to .roll inside the contextmenu handler but after the return false, so that .on() statement will never run. If that's just because you've only put some of your code in the question please show a more complete sample. And please describe clearly what the desired effect is. Commented Oct 4, 2013 at 9:17
  • Did you write these events in your AJAX success?? Commented Oct 4, 2013 at 9:17
  • You also gotta use http://api.jquery.com/live/ to see whether you can do your stuff after the call on DOM elements . Commented Oct 4, 2013 at 9:19
  • 1
    @TheDarkKnight - Are you aware that .live() was removed from jQuery in v1.9? Use the delegated form of .on() instead (assuming a delegated handler is appropriate here, which is hard to tell from the limited description in the question). Commented Oct 4, 2013 at 9:21

2 Answers 2

1

There's one point that you make sure that if you are binding event on to the element that is dynamic then you should reference it through some static data which is already on the page like body or document. Example :

 $('body').bind('contextmenu','img', function(e) {
 return false;
 /*rollover*/
 $('body').on('mouseenter mouseout','.roll', function() {
 var original = $(this).attr('src');
 var replacement = $(this).data('hoverimg');
 $(this).attr('src', replacement).data('hoverimg', original);
 });
 });

Try running it now. It will definitely work.

answered Oct 4, 2013 at 9:22
Sign up to request clarification or add additional context in comments.

1 Comment

Pleasure is mine my friend.
1

If I understand correctly, you need to bind this only on ajax success. Then you can try binding and unbinding in jQuery.

unbind before calling the ajax and bind it back in the success callback of ajax.

However I would prefer to use .on() and .off() event handler attachments.

See this SO answer for .on VS .live VS .bind

answered Oct 4, 2013 at 9:22

Comments

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.