user1099862
user1099862

Reputation: 123

Jquery Find Using Wildcard

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

Answers (5)

Downpour046
Downpour046

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?

http://jsfiddle.net/HYr2V/

Upvotes: 0

Stefan
Stefan

Reputation: 5662

Try this selector;

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

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

Upvotes: 0

ShankarSangoli
ShankarSangoli

Reputation: 69915

Try this.

$("#box5").find("[id^='chart']").attr("id");

Upvotes: 1

Willem Mulder
Willem Mulder

Reputation: 13994

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

should work.

Upvotes: 5

Samich
Samich

Reputation: 30145

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

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

Upvotes: 0

Related Questions