Jimmy, I don’t see how that is an improvement. Doing $(‘.next’) creates an anonymous jQuery object anyway, so “caching” it isn’t necessary, as it is only used once. The only thing that could be improved in rajug’s contribution is to store $(‘.previous’) in a variable. I also don’t see how creating nextAnchor is an improvement - surely the use of $(this) is sensible?
Are you sure that’ll work? Won’t that just set the href attribute of all the nextAnchors to #sheep every time a “next” link is clicked? This is why I think you actually need $(this). If you’re worried that find() is slow, then doing $(this).firstChild() would be a suitable alternative.
Raffles, nothing such special reason but to avoid the animations since the function’s first argument can be a string or number (milliseconds) and the 0 here means without any animation. I normally pass it instead of the string ‘fast’.
Same as show( speed, [callback] ) without animations.
So there is no need to pass anything if you want no animation. And I don’t know what you mean about passing milliseconds to the function being a problem… if you’re wondering what it’ll do with 0, then it’ll just be processed by the browser (via setInterval or setTimeout) as fast as it can. It often depends on the browser, if I remember correctly Safari makes 20ms the limit for intervals or timeouts for example.
And I wouldn’t worry much about caching in this instance, but it’s always worth doing for things like $(“.previous”) if you’re going to reuse the same collection of elements.
Umm… do you mean if I pass 0 to the function the browser will has to process the value and it may take time and be slower? I was not aware of it. My only thinking is if I give 0 to the function then it will be faster (fastest one actually).
I think it needs some benchmarking or so and will try to do it later when become free. But here I don’t mean that we must pass 0. This is the way what I normally used to do with the show/hide methods of jquery.
Agreed! In this instance I did not use it storing the jquery object in a variable because there only two uses of it.
All I mean is you don’t need to pass anything at all to it. Read the documentation! That’s what it says - without any parameters it’ll just show/hide without animations.
I think what I’m trying to achieve is a little more complicated then this.
I have four DIVs spaced out across the page. I’m trying to get the first next link to point to DIV #two and once this is done, the link will change to href=“#three” and the previous link will appear. Does that make more sense?
From the progress of your method changing the href on first click jumped to #three intstead of #two and after much reading up I couldn’t really work out what was wrong. xD
Javascript really isn’t my strongest point.
However the link you just posted looks like it could do the job. Thank you!
As far as I know, this.hash should not be anything, especially as in the jQuery context this doesn’t refer to the element ($(this) does).
Looking at the documentation, the first argument should be a DOM element to scroll to or a number, among other things. So you will want to use $($(this.attr('href')) instead of this.hash.
I’m not entirely sure this is the issue, but I can’t see anything else that looks amiss.
BTW, your wire.png image is far too big. It takes ages to download, many visitors will lose patience - I was wondering what on earth was going on (although I suppose it’ll be less bad once you have some more content there). Still, you have to try to get it below about 100K. Reduce the quality, make it shorter and repeat it, and perhaps try using pngcrush.