tags:

views:

37

answers:

2

How can I use jQuery to obtain all the ids of the anchors within a specific div and convert them to a array for passing over a load/get method?

The HTML will be in the following format:

<div id="a div id">
   <div style="some styling">
      <span><a href="" id="the_id_1"/></span>
      <span><a href="" id="the_id_2"/></span>
      <span><a href="" id="the_id_3"/></span>
   </div>
</div>

So in theory I'm after passing the ids of the anchors over a load method so I can refresh the content of a div based on the selected ids.

Edit #1

The id is slightly different from what I mention above.

Here is the function I'm using to obtain the id number from the anchors:

function getSelectedTierPanels(tierId) {
    var container = $('#tier'+tierId+' a').map(function() { 
        return this.id.match(/\d+$/);
    }).get();
    alert(container);
    return container;
}
+3  A: 

Something like this should work:

var anchors = [];
$('#a_div_id a').each(function() {
    // `this` is the actual `a` element
    if (a.id) {
        anchors.push(a.id);
    }
});

The selector "#a_div_id a" (note I've changed the ID a bit to make it work; no spaces allowed in IDs for use in selectors) looks for all a elements that are descendants of the element with the ID "a_div_id". jQuery's each loops through them, with each call to the iterator function setting this to the actual a element (not a jQuery object for it). From the a element instance, you can query the id property, which is a reflection of the id attribute.

T.J. Crowder
+1, I'm fairly certain this would be a little faster than *map()* if speed was an issue.
Andy E
@Andy E: (I see you've dropped the "head" -- good move. ;) ) Cheers. Of course, if speed is *really* an issue, you'd want to drop the `each` out as well and use a boring loop. :-)
T.J. Crowder
+3  A: 

Similar to TJ's answer, but uses the map() function to create a new jQuery object containing all the IDs, followed by get() to get a normal array from the object:

var anchors = $('#a_div_id a').map(function () { return this.id; }).get();


To get just the number from the id in the format the_id_1, the_id_2, you can use one of my favourite techniques - splitting and popping:

var anchors = $('#a_div_id a').map(function () { 
    return this.id.split("_").pop();
}).get();

This technique splits the string into an array at each _. The pop() method removes and returns the last item of the array, which is just the number in this case. If you wanted a different part of the array, you could refer to the item's index directly.


If your id is slightly different, then a regular expression might be better than splitting. What you have currently is return this.id.match(/\d+$/);, which is just fine except that match() returns an array so you need to access the first element of that array to retrieve the full match:

return this.id.match(/\d+$/)[0];

Note that this would throw an error if there was no successful match, so you're better off storing match to a variable:

var match = this.id.match(/\d+$/);
return match ? match[0] : null;
Andy E
My anchor ids are are alpha-numeric representation of a record id, e.g. "the_id_1_" 1 would be the record id. How can I only import this into the array. I like both methods but Andy E yours seems less of a foot-print.
@user: I've updated my answer with an example on how to just get the number for you.
Andy E
Hmm this doesn't seem to work in IE 8. I'm getting NaN
@user: it doesn't make sense that you would get `NaN` here, there are no number operations included in this code. Are you sure the id is of the format `the_id_1` ?
Andy E
I'll amend my original post to show the function I'm using.
@user: I've updated my answer again :-)
Andy E
Ah I see, so in theory nothing was being returned. I've added the return match ? statement as initially my function won't return anything.
I've noticed as well that this appears to be returning the id of my container div as well I.e. <div id="a div id"> as part of the array.
Ah it's because when I generate new blocks after the initial one, I add a remove link with an id. See amended post.
i fixed the problem by on targeting links with a specific class which I already had in place.
@user: glad you got it sorted, good job :-)
Andy E