Dave's Brain

Browse - programming tips - jquery get id of elements using a css class

Date: 2009dec16
Language: javaScript
Platform: 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>');
		}
	}
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2010, 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.