CameoCream CameoCream - 5 months ago 9
jQuery Question

Turn a class ID into an variable in jQuery

How can I made the ID of an div class/id in HTML into a variable in Java/jQuery?

So say for example I have the code below

<div id="wrapper">

<div id="slider">

<div class="slide" id="slide1ID"> <a href="link"> <img src="imglink">

</a>
</div>
</div>
</div>


How would I reference the id of "slide1ID" in jQuery? Would it be something similar to this?

var ID-of-Slide = 'id=slide1ID'

Answer

You can do:

var ID-of-Slide = $('.slide').attr('id');

or more specific:

var ID-of-Slide = $('#wrapper .slide').attr('id');

Update:

You can use map() to get an array of ids when you have multiple classes of slide:

var idArray = $("#wrapper .slide").map(function() { return this.id; }).get();