0

In my HTML page DOM what I want to achieve is there is <li> tag with id 'liPref' and I want to search if that 'lipref' element has some element whose id contains string 'dlpref' as its inner HTML.

I am not getting any idea how do I achieve that using jQuery selectors.

MaxRecursion
  • 4,773
  • 12
  • 42
  • 76
  • As a start: [Find all elements on a page whose element ID contains a certain text using jQuery](http://stackoverflow.com/q/1206739/218196) – Felix Kling Jan 20 '14 at 06:53

3 Answers3

2

Try this:

$("#liPref").find("[id*='dlpref']").each(function(){
//do something here
});

For getting length:

 $("#liPref").find("[id*='dlpref']").length;

attribute-contains-selector

Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
1

Try to check whether the liPref element has a an element with id containing the given string

if($('#liPref').has('[id*=dlpref]').length){
   alert('foune')
}

.has(), attribute contains

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • @FelixKling no.. it is a filter... `is` returns s boolean... `Reduce the set of matched elements to those that have a descendant that matches the selector or DOM element.` - the is() implementation can be `$('#liPref').is(':has([id*=dlpref])')` – Arun P Johny Jan 20 '14 at 06:50
  • Uh sorry. That's one of those ambiguous method names :) – Felix Kling Jan 20 '14 at 06:51
  • @FelixKling yes... the name looks like it returns a boolean value... I had the same doubt – Arun P Johny Jan 20 '14 at 06:53
1

You can try to use attribute contains *= selector:

if($("#liPref [id*='dlpref']").length) { 
   // Existed
}
Felix
  • 37,892
  • 8
  • 43
  • 55