tags:

views:

102

answers:

3

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.

+8  A: 

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.

Coronatus
I'm inclined to agree. Adding a function would mean that there are now two different ways to do it, and I favor consistency.
Robert Harvey
every time I have to write `$('.someSelector').attr('id')` takes up space too. Why do they have `size()` when `length` does the exact same thing? Why do they have `$('div').contains('text')` when `$('div :contains(text)')` does the same exact thing?
Jason
@Jason: Well, `$('div, span:contains(text)')` and `$('div, span').contains('text')` obviously give you different results. `size()` might be due to historic reasons. Btw if it really bothers you that much... just write the function yourself ;)
Felix Kling
contains is used for identifying subsequent dom elements. There is already an id selector, so I'm not sure that argument really holds water. In one example you're creating a selector, in the other you're identifying an attribute.
tzenes
@Felix `size()` returns the number of elements matched, not the size of the dom object
tzenes
@tzenes: The documentation says: *The .length property is a slightly faster way to get this information.* http://api.jquery.com/size/
Felix Kling
@felix so what you're saying is that jquery created the `.size()` function for absolutely no reason....
Jason
@Felix `size()` is a method, `length` is an member, the implementation is different. Since functions are first class objects in javascript it is reasonable to have both
tzenes
@Jason: Well I don't what they had in mind when they created jQuery **1.0**. From a comment on the `size()` documentation: *Yeah, this method is a "historical artifact" of sorts. The idea of deprecating it has been kicked around before,[...]* . I assume most libraries have such artifacts which they cannot just remove due to compatibility reasons.
Felix Kling
@tzenes: I don't argue about the use or usefulness. I just noted that `.length` and `.size()` return the same value. I thought your first comment to my comment was targeting at that. If not, I don't know what you want to say to me with *size() returns the number of elements matched, not the size of the dom object* ;) . It does not seem to relate to any of my comments.
Felix Kling
@Felix I misread your original comment based on the first sentence it sounded like you were suggesting that `size()` was functioning like `$().attr('size')`. It was just a little miscommunication, mia culpa.
tzenes
+6  A: 

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;

James Westgate
ah, this is a good reason.
Jason
+1  A: 

I think that if you really want and need that functionality to save time, I would just add it as a new method. Something along the lines of:

$.fn.extend({
    id: function(){return this.attr('id');}
});
$('.test').id();

But I will echo the same concern that James Westgate said, if you have multiple returns in the collection, you need to make sure that you handle it properly or else you could break stuff.

FallenRayne
thanks for the example. i think it could be helpful when you're expecting only a single element to be returned, but i do see the implications if multiple are. is there a better way to retrieve the id or is .attr('id') pretty much the only way?
Jason
I believe .attr('id') is the easiest way. There are other ways but none that I would ever recommend. If you want to make sure that this is only ever used on one returned element you can add in a check for that. I haven't really played around with this too much, I just know it exists.
FallenRayne