CameoCream CameoCream - 1 year ago 59
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'

Answer Source

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();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download