Dave's Brain

Browse - Programming Tips - How can get the id of every element using a certain class (using jQuery)?

Date: 2009dec16
Language: javaScript
Library: jQuery

Q.  How can get the id of every element using a certain class (using jQuery)?

A.  This is the kind of thing jQuery was made for.  Here is a function
that returns an array of id of all elements using a certain class.

function getIds(selector) { var a = []; $(selector).each(function() { a.push(this.id); }); return a; } function exampleUse() { var a, i; a = getIds('.myclass'); for (i = 0; i < a.length; i++) { document.writeln('id: ' + a[i] + '<br>'); } }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.