Text () in jquery, HTML () and Val () differences

Source: Internet
Author: User

Definition and usage

The text () method method Sets or returns the textual content of the selected element

The code is as follows

<!DOCTYPE HTML><HTMLLang= "en"><Head>    <MetaCharSet= "UTF-8">    <Metaname= "Viewport"content= "Width=device-width, initial-scale=1.0">    <Metahttp-equiv= "X-ua-compatible"content= "Ie=edge">    <Scriptsrc= "./js/jquery-1.12.4.js"></Script>    <title>Document</title></Head><Body>    <DivID= "Div1">Div has textual content</Div>    <DivID= "Div2">text within the Div2<span>Text content within span</span>    </Div>    <inputtype= "text"ID= "INPUT1"value= "This is an input tag">    <inputtype= "text"name=""ID= "Input2"placeholder= "Can be obtained successfully">    <ButtonID= "Button1"value= "This is a button tag"></Button>    <Script>Console.log ($ ("#div1"). text ()); Console.log ($ ("#div2"). text ()); Console.log ($ ("#div2 span"). text ()); Console.log ($ ("#input1"). text ()); Console.log ($ ("#input2"). text ()); Console.log ($ ("#button1"). text ()); </Script></Body></HTML>

Results of console printing

It can be seen that text () only output the contents of the label, and JS InnerText method

Definition and usage

The HTML () method returns or sets the content of the selected element (inner HTML), including the label.

If the method does not set a parameter, the current contents of the selected element are returned.

<Body>    <DivID= "Div1">Div has textual content</Div>    <DivID= "Div2">text within the Div2<span>Text content within span</span>    </Div>    <inputtype= "text"ID= "INPUT1"value= "This is an input tag">    <inputtype= "text"name=""ID= "Input2"placeholder= "Can be obtained successfully">    <ButtonID= "Button1"value= "This is a button tag"></Button>    <Script>Console.log ($ ("#div1"). HTML ()); Console.log ($ ("#div2"). HTML ()); Console.log ($ ("#div2 span"). HTML ()); Console.log ($ ("#input1"). HTML ()); Console.log ($ ("#input2"). HTML ()); Console.log ($ ("#button1"). HTML ()); </Script></Body>

Results of printing through the console

Prints the contents of the text within the current label, and if there is a child tag, the label itself and the text inside the child label are printed together

This is the same as JS innerHTML.

Definition and usage

The Val () method returns or sets the value of the selected element.

The value of the element is set through the Value property. This method is mostly used for input elements.

method is used primarily to get the value of a FORM element

If the method does not set a parameter, the current value of the selected element is returned.

<Body>    <DivID= "Div1">Div has textual content</Div>    <DivID= "Div2">text within the Div2<span>Text content within span</span>    </Div>    <inputtype= "text"ID= "INPUT1"value= "This is an input label 1">    <inputtype= "text"name=""ID= "Input2"value= "This is an input label 2"placeholder= "Can be obtained successfully">    <ButtonID= "Button1"value= "This is a button tag"></Button>    <Script>Console.log ($ ("#div1"). Val ()); Console.log ($ ("#div2"). Val ()); Console.log ($ ("#div2 span"). Val ()); Console.log ($ ("#input1"). Val ()); Console.log ($ ("#input2"). Val ()); Console.log ($ ("#button1"). Val ()); </Script></Body>

Print results from the console

Val () is used to output the data in the form, you can see that the text within the div and span tags is not output, and I have also tested H5 new tags placeholder

is also not output, so this val should be only for the label's Value property

Text () in jquery, HTML () and Val () differences

Related Article

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: info-contact@alibabacloud.com 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.