Anshul
Anshul

Reputation: 645

How to Get the ids of span tags

I have a set of the span tags like .

 <span id="q_1" >D1</span>
 <span id="q_2" >D2</span>
 <span id="q_3" >D3</span>

How can i get the ids of the span tags with the help of the jquery.The numbers 1,2,3 are generated run time.so the basic structure that i have is

<span id="q_" ></span>

Upvotes: 0

Views: 180

Answers (5)

herostwist
herostwist

Reputation: 3968

to get an array if ids:

var a = $.map($('span'), function(s){ return s.id; });

Upvotes: 2

mVChr
mVChr

Reputation: 50215

I have a feeling you're actually asking how to select all elements that have an id that starts with "q_". If so, the simplest way to do so is like this:

var qSpans = $('span[id^="q_"]');

See example →

Upvotes: 2

Alex
Alex

Reputation: 2122

$('span');

Will find all spans.

$('span').filter(function() {
    return (/^q\_\d+$/i).test($(this).attr('id'));
});

Or only spans who's id matches the format

Upvotes: 1

Calum
Calum

Reputation: 5326

$('span').each(function(){
   alert($(this).attr('id'));
});

Upvotes: 3

Yngve B-Nilsen
Yngve B-Nilsen

Reputation: 9676

$("span").each(function(){
     var thisId = $(this).attr('id');
     // Do whatever you want with the Id, and go on to the next one.
});

:)

Upvotes: 6

Related Questions