Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't there a $('.someSelector').id() method in jQuery?

Tags:

jquery

I was wondering if anyone knew why jQuery doesn't have a simple $().id() method. It seems silly to have to pull the id using $().attr('id'). I would think that the id attribute was common and useful enough to have its own call.

like image 514
Jason Avatar asked Dec 05 '22 03:12

Jason


2 Answers

Why clutter up the namespace with superflous functions? You already said attr() can do the same thing in only a few extra characters. For every extra function, it adds bytes to the file size, and time to the JavaScript parser.

like image 189
Amy B Avatar answered Jan 29 '23 03:01

Amy B


Another problem is the jQuery returns a matched set, so typically you could have more than one id. By implementing an Id function, you could be breaking chainability by returning the id of the first item in a set.

You could simply use $('selector').get(0).id;

like image 37
James Westgate Avatar answered Jan 29 '23 03:01

James Westgate