Jump to content


Photo

Help Me with JQUERY attr please


  • Please log in to reply
1 reply to this topic

#1 jbwebdesign

jbwebdesign

    Advanced Member

  • Member
  • PipPipPipPip
  • 168 posts
  • LocationNew York

Posted 25 March 2012 - 09:54 AM

hello, i need help with a code that i am trying to work on. It doesn't seem to work for me at all.....

i am using JQUERY SLIDE EFFECTS

and i can't seem to get this to work how i want it to.

Basically i want to get the image to swap back to "bear.gif" when the effect has finished.

Here is my code...

$(window).load(function() {
        $('#slider').nivoSlider();
		
		//polar bear menu action <img src='http://www.killersites.com/community/public/style_emoticons/<#EMO_DIR#>/smile.gif' class='bbc_emoticon' alt=':)' />
		$("#logo").toggle(function(){
			 $("#polarbear a img").attr('src','images/polarbearcub.gif');
			 $("#menu").show('slide', {direction: 'left'}, 3000); 
			 
			 //after animation is complete i want to change polarbear image src back to images/bear.gif
			 $("#polarbear a img").attr('src','images/bear.gif');
			 			 			 	
		},function(){
			$("#polarbear a img").attr('src','images/polarbearcub.gif');
			$("#menu").hide('slide', {direction: 'left'}, 3000);
		});
		
		
    });

  • 0

#2 mindgrinder

mindgrinder

    New member

  • New Members
  • 9 posts

Posted 25 May 2012 - 11:17 AM

I don't believe attribute is being used properly:

As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. In addition, .attr() should not be used on plain objects, arrays, the window, or the document. To retrieve and change DOM properties, use the .prop() method.

Specially because you are using window, what about document.ready() instead.


"Launching Code on Document Ready

The first thing that most Javascript programmers end up doing is adding some code to their program, similar to this:

window.onload = function(){ alert("welcome"); }

Inside of which is the code that you want to run right when the page is loaded. Problematically, however, the Javascript code isn't run until all images are finished downloading (this includes banner ads). The reason for using window.onload in the first place is that the HTML 'document' isn't finished loading yet, when you first try to run your code.

To circumvent both problems, jQuery has a simple statement that checks the document and waits until it's ready to be manipulated, known as the ready event":


$(document).ready(function(){
// Your code here
});



hope that helps
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users