CameoCream CameoCream - 8 months ago 19
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">


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

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


You can do:

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

or more specific:

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


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

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