JQuery, returning a list of string IDs of all elements with an identifier that "starts with" a string?

I am not a Javascript guru as I am doing more server side work, so I struggle with this. I found pieces of how to do this. Basically, I have a number of elements that have identifiers starting with the string "tagRow_", and I need to return a list of all the actual identifiers of the elements, since I need not only the element, but also the identifier of the element, since I need to analyze a unique end for each on the server side to determine what matches it.

I found the code below to get all the elements, but I'm not sure if it returns to the list, or that if anyone can offer advice on how to return only a list of string identifier names, I would appreciate it. Thanks

EDIT: I really need to do this with a radio input field, by mistake I put a DIV in my own example. It works fine for a DIV, but does not work properly for a radio input, as shown below:

 <input id="tagRow_ddd" type="radio" value="h">
                <input id="tagRow_ddd" type="radio" value="p">
                    <input id="tagRow_ddd" type="radio" value="r">
                    <input id="tagRow_ddd" type="radio" value="c">

$("input[id^='tagRow_']").each(function() {

   var id = this.id,
       idNumber = id.replace(/\D+/, '');
   document.body.innerHTML += idNumber + '<br />';
});

http://jsfiddle.net/fD7fP/3/

+3
source share
5 answers

Live demo

var elements = [];

$("div[id^='tagRow_']").each(function(){
   elements.push(this.id); 
});

var stringOfElementIDs = elements.toString();
+13
source

Try the following:

var ids = $("div[id^='tagRow_']").map(function() {
    return this.id;
}).get();

Returns an array of all identifiers. Additional information from this publication: Use jQuery to retrieve data from HTML lists and tables .

+13
$("div[id^=tagRow_]").each()....

:

$("div[id^='tagRow_']").each(function() {    
    alert(this.id);
});
+1

.

$("div[id^='tagRow_']").each(function() {

   var id = this.id,
       idNumber = id.replace(/\D+/, '');

   // idNumber contains the digits that were in the id

});

jsFiddle.

+1

This will return a list of matching DOM elements (like jQuery objects). So you have to go through it and put the identifiers in a separate array.

var matching_els = $("div[id^='tagRow_']"), ids = [];
for(var i=0; i<matching_els.length; i++){
  ids.push(matching_els[i].attr("id");
}
// send ids back to server
0
source

All Articles