Jquery selector doesn't work when html is replaced?

Use delegate() (docs) to manage events inside the divid container.

Up vote 1 down vote favorite share g+ share fb share tw.

Please, help me with following problem: {some default content} {some content 1} {some content 2} {some content 3} {some content 4} {some content 5} $('#divid').hide(). Html(elem.html()).fadeIn(); $('img. Img').

Hover(function () { $(this). Attr('src', 'GoNextArrowHover. Jpg'); }, function () { $(this).

Attr('src', 'GoNextArrow. Jpg'); }); This works well for the first div divid, but doesn't for divid1-divid5 when these are loaded instead of divid. Where is my mistake?

Thanks! Jquery html selector link|improve this question asked Jan 22 '11 at 21:22Denis Monn1516 33% accept rate.

Can you implement you code demonstrating the problem in jsfiddle.net to aid in debugging please – Hawxby Jan 22 '11 at 21:27.

Use delegate()(docs) to manage events inside the divid container. $('#divid'). Delegate('img.

Img','mouseenter',function () { $(this). Attr('src', 'GoNextArrowHover. Jpg'); }) .

Delegate('img. Img','mouseleave', function () { $(this). Attr('src', 'GoNextArrow.

Jpg'); }); Or if possible, make the an element instead, and just do it with CSS using the background-image property: #divid > div > a { background-image:url(GoNextArrow. Jpg); background-repeate:no-repeat; } #divid > div > a:hover { background-image:url(GoNextArrowHover. Jpg); } You'll need a few more properties to make it just right.

Using .delegate() is a good idea... I'm beginner in jquery, I didn't know about .delegate(). Thanks! – Denis Monn Jan 22 '11 at 21:44 1 @Denis: You're welcome.

You can always use .delegate() in place of .live(). They both use event delegation, but .delegate() let's you focus it on a particular portion of the page so it is a more efficient approach. – user113716 Jan 22 '11 at 21:46.

Instead of re-binding .hover() every time, just use .live() and it will work for all current and future elements that match your selector, example... $('. Hoverme'). Live('hover', function(event) { if (event.

Type == 'mouseenter') { $(this). Attr('src', 'GoNextArrowHover. Jpg'); } else { $(this).

Attr('src', 'GoNextArrow. Jpg'); } }).

Your second function your passing will have no effect because .live() can accept only one handler. – user113716 Jan 22 '11 at 21:46 I don't meant to pick on you ;o), but should probably be noted that your update will not work after jQuery 1.4.2. For some bizarre (IMO) reason they started reporting the event as mouseenter in 1.4.3.

– user113716 Jan 22 '11 at 21:55 1 fixed, thanks! – jondavidjohn Jan 22 '11 at 22:21.

When you use .html() to replace the contents of an element, you are creating new child elements (img in your case), so you need to rebind your hover handler to the new img element after each call to .html().

Use jQuery live to bind the hover attempt. Live mens that the function will apply to all future elements that match the selector too. api.jquery.com/live/ $(selector).

Live('hover', function() {}).

This won't work because hover isn't a real event; moreover, hover requires two callbacks while live takes only one. – casablanca Jan 22 '11 at 21:32 It can in the latest version of jquery – Fiona Holder Jan 22 '11 at 21:33 From the site: As of jQuery 1.4.1 the hover event can be specified (mapping to mouseenter and mouseleave, which, in turn, are mapped to mouseover and mouseout). – Fiona Holder Jan 22 '11 at 21:34 GREAT!

THANK YOU SO MUCH! Live() works great! – Denis Monn Jan 22 '11 at 21:35 @Devbook.co.

Uk: You're right, it will work. – casablanca Jan 22 '11 at 21:35.

You might look into using replaceWith(): $('#divid').hide(). ReplaceWith( elem.fadeIn() ); If you've already bound functions to events on elem, those should persist even after you've swapped elem for another element on the page. Example: jsfiddle.net/redler/eLD9A.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions