Reputation: 497
<ul>
<li>
<div class="link" id="contentLink20000002">
<a href="Link1" target="_blank">Link 1</a>
</div>
</li>
<li>
<div class="link" id="contentLink1000002">
<a href="Link2 target="_blank">Link 2</a>
</div>
</li>
<li>
<div class="link" id="contentLink2000003">
<a href="Link3" target="_blank">Link 3</a>
</div>
</li>
<li>
<div class="link" id="contentLink2000004">
<a href="Link 4" target="_blank">Link 4</a>
</div>
</li>
</ul>
I have this structure an I am trying to separate id's which starts with 'contentLink2'. I have tried achieving this with .contains and regex but no luck so far.
var listids = $('ul li div');
listids.each(function(li){
var contentId = $(this).filter('contentLink2').attr('id');
console.log(contentId);
});
What i am trying is to create navigation. Like
Text
link1
link2
Text
link3
link4
HTML is dynamic so I don't have control it.
Upvotes: 0
Views: 4584
Reputation: 43
var x = document.getElementsByTagName('DIV');
for(i = 0 ; i < x.length ; i++){
var y = document.getElementsByTagName('DIV')[i];
var z = y.getAttribute('ID');
var m = z.search('contentLink2');
if(m == -1){
// NO THING
}else{
// Do Some Thing Here
};
}
I guess this what you are looking for.
Upvotes: 0
Reputation: 171679
Can do this with a jQuery attribute selector
listids.filter('[id^=contentLink2]').doSomething()
Upvotes: 1
Reputation: 13966
just use the attr to get to the id
var listids = $('div.link');
listids.each(function(index, element){
var contentId = $(this).attr('id');
// or use the second paramater to access the element
// var contentId = $(element).attr('id');
console.log(contentId.indexOf('contentLink2') !== -1);
});
Upvotes: 1