aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes jquery selecting an element query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "jquery selecting an element query" Watch "jquery selecting an element query" New topic
Author

jquery selecting an element query

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1050

Do they mean the same thing? if yes in the 4 th line why array[0] is being used? because someElement is ID as # is prefixed to it right? and someElement cannot be duplicate.



http://www.lifesbizzare.blogspot.com || OCJP:81%
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60082
    
  65

They do the the same thing, but do not mean the same thing.

The only reason that they do the same thing is the mere fact that an element id can only be used once in a page. If you take the same example, and use a different selector:
They will not do the same thing if there is more than one element with class some-class.

The first statement will apply to all the elements, the second will only apply to the first.

The second statement, by the way, would be considered poor practice. When using jQuery, use jQuery.

The [0] in the second statement takes advantage of the fact the the jQuery wrapped set created by the $() function acts like an array, and so [0] will fetch the first element from the wrapped set if one exists.

It's better to use the .get() wrapper method to fetch an item from the wrapped set.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60082
    
  65

One more point:

Even when there is only one element in the wrapped set, the expressions:
are not all the same.

The first returns a jQuery wrapped set that contains the single element.

The second fetches the first element from the wrapped set and returns a reference to that element; not the wrapped set.

The third does the same as the second, but using a method that is generally preferred over array notation.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60082
    
  65

Another point, with code such as:If the element does not exist, the first format will do nothing, while the second format will result in a JavaScript error.

Bottom line: use jQuery as intended.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jquery selecting an element query
 
Similar Threads
Getting the form elements' IDs in Netscape
adding more attributes+DOM
When does livequery execute code?
how to get id of many many list within ace:dialog and c:foreach
A Heap O� Trouble :-- Head First Java