0

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> 
0

5 Answers 5

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

:)

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

2

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 →

1 Comment

Many questions on stackoverflow seem to involve pressing keys randomly until satisfied :)
2

to get an array if ids:

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

1 Comment

var a = $.map($('span[id^="q_"]'), function(s){ return s.attr('id'); });
1
$('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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.