On 8/22/07, Neal <[EMAIL PROTECTED]> wrote: > > > I'm having problems iterating through an array of JQuery objects. > Here's my code: > var x = $("#skip a"); > > for (var i=0;i<a.length;i++){ > var n = x[i]; > $(n).click(function(){ > $(n.hash).hide(); > $("#primary h1").after( $(n.hash)[0] ); > $(n.hash).fadeIn("slow"); > return false; > }); > } > > x=$("skip a) is an array of links which have named anchors for their > href. I want each link to not jump to the named anchor, but to > dynamically change the page layout by moving the named anchor to a new > area of the page. > > But each link moves the same named anchor. what am I doing wrong? > > First, in your for loop, did you mean 'i < x.length' instead of a.length? That's the only mention of 'a' I see.
Assuming, you meant 'x', let's break this down a little. Your for loop can be replaced by .each, and 'this' refers to each DOMElement: $("#skip a").each(function(i) { var n = this; ... }); Since all you're doing inside .each is calling .click, .each can be replaced by .click (which will in turn call .each): $("#skip a").click(function() { var n = this; ... }) That should get us here: $("#skip a").click(function() { var toMove = $(this.hash); toMove.hide(); $("#primary h1").after(toMove); toMove.fadeIn("slow"); return false; }); I assume .hash is an expando property on your anchor? Or maybe you want $(this).attr('href')? A little more slimming: $("#skip a").click(function() { $( $(this).attr('href') ).hide().insertAfter("#primary h1").fadeIn("slow"); return false; }); The above code is tested and works assuming your named anchors have ids with the same value as name. - Richard