vsoni
vsoni

Reputation: 497

filter div from their id using jquery

 <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

Text

HTML is dynamic so I don't have control it.

Upvotes: 0

Views: 4584

Answers (3)

peter hany
peter hany

Reputation: 43

use JS instead

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

charlietfl
charlietfl

Reputation: 171679

Can do this with a jQuery attribute selector

listids.filter('[id^=contentLink2]').doSomething()

Upvotes: 1

Nerdroid
Nerdroid

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

Related Questions