Why is there no $ ('. SomeSelector') method in jQuery. Id ()?

I was wondering if anyone knows why jQuery doesn't have a simple $().id() method. It seems silly to pull an identifier with $().attr('id') . I would think the id attribute was general and useful to have my own call.

+4
source share
3 answers

Another problem is that jQuery returns a consistent set, so usually you can have more than one identifier. By implementing the Id function, you can break the chain by returning the identifier of the first element to the set.

You can just use $ ('selector'). get (0) .id;

+7
source

Why clutter the namespace with super flat functions? You have already said that attr() can only do the same with a few extra characters. For each additional function, it adds bytes to the file size and time for the JavaScript parser.

+8
source

I think that if you really want and need this function to save time, I would just add it as a new method. Sort of:

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

But I will repeat the same concern that James Westgate said, if you have several returns to the collection, you need to make sure that you handle it properly, otherwise you may break the material.

+1
source

Source: https://habr.com/ru/post/1306310/


All Articles