Reputation: 123
I need to find the id of a div that starts with chart using 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 doesn't seem to work.
Thanks for the help,
Upvotes: 4
Views: 12048
Reputation: 1671
Your code appears to be working fine for me in jQuery 1.7.1:
Please take a look at this jsFiddle. Is your code in the document ready?
Upvotes: 0
Reputation: 5662
Try this selector;
$('#box5 div[id^="chart"]')
Reference; http://api.jquery.com/attribute-starts-with-selector/
Upvotes: 0
Reputation: 13994
var found = $('div#box5').find("[id^=chart]").attr('id');
alert(found);
should work.
Upvotes: 5
Reputation: 30145
var found = $('div#box5').find('div[id^=chart]');
alert(found);
Code: http://jsfiddle.net/KQ9fR/4/
Upvotes: 0