The HTML (), text (), Val () difference in jquery

Source: Internet
Author: User

In jquery. HTML () is used for reading and modifying elements of the HTML tag,. Text () to read or modify the plain text content of the element,. Val () is used to read or modify the value of the form element.

1.HTML

HTML (): Gets the HTML content of the first matching element. This function cannot be used with XML documents. But can be used in XHTML documents

HTML (val): Sets the HTML content of each matching element. This function cannot be used with XML documents. But it can be used in XHTML documents.

2.TEXT

Text (): Gets the contents of all matching elements.

The result is text that is combined with the text content contained by all matching elements. This method is valid for both HTML and XML documents.

Text (val): Sets the textual content of all matching elements

Similar to HTML (), but encodes HTML (replaces "<" and ">" with the corresponding HTML entity).

3.VAL

Val (): Gets the current value of the first matching element.

Val (val): Sets the value of each matching element.

The above content is copied in the jquery help document.

Finally, summarize these three methods:

  1. . HTML () HTML tags for reading and modifying elements
  2. . Text () to read or modify the plain text content of an element
  3. . Val () is used to read or modify the value of the form element.

The comparison of these three methods in function:

  1. . HTML (),. Text (),. Val () Three methods are used to read the contents of the selected element;
  2. Just. HTML () is used to read the HTML content of an element (including its HTML tag), and. Text () is used to read the plain text content of an element, including its descendant elements,. Val () is a value that is used to read the form element's "value".
  3. The. and. Text () methods cannot be used on form elements, while. val () can only be used on form elements;
  4. In addition, when the HTML () method is used on more than one element, only the first element is read;
  5. The. Val () method is the same as. html (), and can only read the value of the first FORM element if it is applied on more than one element, but. Text () is not the same as they are, if. Text () is applied on more than one element, the text content of all the selected elements is read.
  6. . HTML (htmlstring),. Text (TextString), and. Val (value) Three methods are used to replace the contents of the selected element, and if three methods are applied on more than one element at the same time, the contents of all the selected elements will be replaced.
  7. . HTML (),. Text (),. Val () can use the return value of the callback function to dynamically change the contents of multiple elements.

< reference: "JQuery". An overview of the HTML (),. Text (), and. Val () and Usage & analysis of the HTML (), text (), Val () difference in jQuery () >

HTML () in jquery, text (), Val () difference

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.