The difference between $ ("#id") in jquery and document.getElementById ("id")

Source: Internet
Author: User

Excerpt from:

It used to be that the $ ("#id") and document.getelementbyidx_x ("id") in jquery had the same effect, and it was not the same thing to do with special effects today, which was tested:

1. Alert ($ ("#div")) gets [object object]

2. Alert (document.getelementbyid ("div")) gets [object htmldivelement]

3. Alert ($ ("#div") [0]) or alert ($ ("#div"). get (0)) get [object htmldivelement]

Explanation of Reason:

document.getElementById () returns a DOM object, while $ () returns a jquery object

What is a jquery object?
---is the object that is generated after the DOM object is wrapped through jquery. jquery objects are unique to jquery and can be used in the jquery Approach.
Like what:
$ ("#test"). HTML () means: Gets the HTML code within the element with ID Test. Where HTML () isthe method in jquery
This code is equivalent to implementing the code with the Dom:
document.getElementById ("id"). innerHTML;
Although jquery objects are created after wrapping a DOM object, jquery cannot use any of the DOM Object's methods, nor does the DOM object use the methods in Jquery. the use of chaos will result in an Error. For example: $ ("#test"). innerHTML, document.getElementById ("id").The syntax of HTML () is Wrong.
It is also important to note that the DOM object obtained by the jquery object and document.getElementById ("id") with #id as the selector is not Equivalent. See the conversion between the two below.
Since jquery is a different but also connected, jquery objects and Dom objects can also be converted to each other. Before we convert the two, we give a contract: if one gets a jquery object, then we add $ to the variable, such as: var $variab = jquery object; If you get a DOM object, it's the same as usual: var Variab = dom Object , so the agreement is only easy to explain and distinguish, the actual use does not Stipulate.

The jquery object turns into a DOM object:
Two conversions convert a jquery object to a DOM object: [index] and. Get (index);
(1) jquery object is a data object, you can get the corresponding Dom object by means of [index].
Such as: var $v =$ ("#v"); jquery Object
var v= $v [0]; Dom Object
Alert (v.checked)//detect If this checkbox is selected
(2) jquery itself provides, through The. get (index) method, to get the corresponding DOM object
Such as: var $v =$ ("#v"); jquery Object
var v= $v. Get (0); Dom Object
Alert (v.checked)//detect If this checkbox is selected

the DOM object is turned into a jquery object:
For a DOM object, you can just wrap the DOM object with $ () and you'll get a jquery object. $ (dom Object)
Such as: var V=document.getelementbyid ("v"); Dom Object
var $v =$ (v); jquery Object
After the conversion, you can use the jquery method arbitrarily.
With the above methods, jquery objects and Dom objects can be converted to each other arbitrarily. It is important to note that DOM objects can use methods in the dom, and jquery objects are not available in the DOM.

Here are some other ways to use it:
1. Dom object to jquery object
Ordinary DOM objects can generally be converted to jquery objects by $ ().

Such as: $ (document.getelementbyid ("msg"))
The jquery object is returned, and the jquery method can be Used.

2. jquery object to DOM object
Because the jquery object itself is a Collection. So if a jquery object is to be converted to a DOM object, it must take one of these items, which is generally available through the Index.
Such as: $ ("#msg") [0],$ ("div"). eq (1) [0],$ ("div"). get () [1],$ ("td") [5]

These are Dom objects, and you can use the methods in the dom, but you can't use jquery any More.

The following are the correct ways to do this:
$ ("#msg").HTML ();
$ ("#msg") [0].innerhtml;
$ ("#msg"). eq (0) [0].innerhtml;
$ ("#msg"). Get (0). innerHTML;

3, two methods to obtain the object Mode: if there is <form name= "formName" id= "formid" ></form>

by id:

document.getElementById ('formid');

$ (' #Formid')

By name

Document.getelementsbyname ('formName') [0]

$ ("form[name= 'formName')")

Jquery's Inherent Approach:

$ ("#id") in jquery differs from document.getElementById ("id")

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.