admin管理员组文章数量:1316673
I want to get the id
of a single <span>
element during iteration.
Example scenario:
I have a few id
s which start with AAA
and end with BBB
. The middle characters can vary:
<span id="AAA-LS-BBB">hi1</span>
<span id="AAA-LAS-BBB">hi2</span>
<span id="AAA-LBS-BBB">hi3</span>
<span id="AAA--LCS--BBB">hi4</span>
Here, first 3 digits of span id and last 3 digits of span id is same...only in middle it will vary. I want to generalized method to know ...each id using... $("#id").attr("id") so, here I am trying to find out iteration like below, I want to write one iterator method to get the current id
$("span[id^='AAA-***-BBB']").each(function(){
var a = $("this").attr("id");
});
Hope my requirements are clear. how ti use regular expression here.. to find out id?
I want to get the id
of a single <span>
element during iteration.
Example scenario:
I have a few id
s which start with AAA
and end with BBB
. The middle characters can vary:
<span id="AAA-LS-BBB">hi1</span>
<span id="AAA-LAS-BBB">hi2</span>
<span id="AAA-LBS-BBB">hi3</span>
<span id="AAA--LCS--BBB">hi4</span>
Here, first 3 digits of span id and last 3 digits of span id is same...only in middle it will vary. I want to generalized method to know ...each id using... $("#id").attr("id") so, here I am trying to find out iteration like below, I want to write one iterator method to get the current id
$("span[id^='AAA-***-BBB']").each(function(){
var a = $("this").attr("id");
});
Hope my requirements are clear. how ti use regular expression here.. to find out id?
Share Improve this question edited Apr 5, 2011 at 4:56 pradeep cs asked Apr 5, 2011 at 4:33 pradeep cspradeep cs 5234 gold badges9 silver badges35 bronze badges 2- What do you mean with print? How? Where? – emco Commented Apr 5, 2011 at 4:37
- Uhh, quite unclear I must say. Either use [jsfiddle](jsfiddle) or update your post with at the very least some HTML and a clearer description of what it is you are trying to achieve. – Khez Commented Apr 5, 2011 at 4:38
3 Answers
Reset to default 9Use .filter()
bined with a regex:
$('span[id]').filter(function () {
return /^AAA.*BBB$/.test(this.id);
}).each(function () {
alert(this.id);
});
Alternatively, since you are using a .each()
already, you can simply add an if
condition inside it:
$('span[id]').each(function () {
if (/^AAA.*BBB$/.test(this.id)) {
alert(this.id);
}
});
Update: It looks like you can also bine both the Attribute-Starts-With and Attribute-Ends-With selectors on the same attribute:
$('div[id^="AAA"][id$="BBB"]').each(function () {
alert(this.id);
});
(See example: http://jsfiddle/2pz8X/)
Combining fellow contributors' suggestions, and making the regex more specific:
// first, at least get all <div>'s with id's that start with 'AAA'
$("div[id^='AAA']").filter(function(){
var regexp = /^A{3}(.*)B{3}$/; // regex
return (this.id.match(regexp));
});
This will return all the div's that you need to target.
Sample @ http://jsfiddle/4ZNWJ/
Try this:
$("span[id^='AAA']").each(function(){
alert($(this).id);
});
本文标签: javascriptIterate through elements with similar IDsStack Overflow
版权声明:本文标题:javascript - Iterate through elements with similar IDs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742001391a2411084.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论