JQuery Find Using Wildcards

I need to find the id div that starts with a chart using a wildcard:

So, given the following DOM

<div id="box5" class="box-container"> <div class="inner-box wide"> <div class="top-box handle">Some content</div> <div class="chart"><div id="chart1_div">A chart</div></div> </div> </div> 

My guess was.

  var $elementToFind = $("[id^=chart]"); var found = $('div#box5').find($elementToFind).attr('id'); alert(found); 

But it doesn't seem to work.

Thanks for the help,

+4
source share
5 answers
 var found = $('div#box5').find("[id^=chart]").attr('id'); alert(found); 

must work.

+5
source

Try it.

 $("#box5").find("[id^='chart']").attr("id"); 
+1
source
 var found = $('div#box5').find('div[id^=chart]'); alert(found); 

Code: http://jsfiddle.net/KQ9fR/4/

0
source

Try this selector;

 $('#box5 div[id^="chart"]') 

Directory; http://api.jquery.com/attribute-starts-with-selector/

0
source

Your code seems to work fine for me in jQuery 1.7.1:

Please take a look at this jsFiddle. Is your code confirmed in the document?

http://jsfiddle.net/HYr2V/

0
source

Source: https://habr.com/ru/post/1395252/


All Articles