Jquery learns the second property text and value (text, val)

Source: Internet
Author: User

Text () gets the content of all matching elements. The result is a combination of text content contained by all matching elements. This method is effective for both HTML and XML documents. Get the text contents of all matched elements. The result is a string that contains the combined text contents of all matched elements. This method works on both HTML and XML documents. Return Value

String

Example

HTML code:

<P> <B> Test </B> Paragraph. </p> <p> Paraparagraph </p>

JQuery code:

$ ("P"). text ();

Result:

Test Paragraph. paraparagraph plain text (val) sets the text content of all matching elements to be similar to html (), but encodes HTML (replaces "<" and ">" with the corresponding HTML Entity ). set the text contents of all matched elements. similar to html (), but escapes HTML (replace "<" and ">" with their HTML entities ). return Value

JQuery

Parameters

Val(String): used to set the text of the element content

Example

HTML code:

<P> Test Paragraph. </p>

JQuery code:

$ ("P"). text ("<B> Some </B> new text .");

Result:

[<P> <B> Some </B> new text. </p>] returns the current value of the First Matching Element. In jQuery 1.2, the value of any element can be returned. Including select. If multiple values are selected, an array containing the selected values is returned. Get the content of the value attribute of the first matched element. In jQuery 1.2, a value is now returned for all elements, including selects. For multiple selects an array of values is returned. Return value

String, Array

Example

Obtain the values of a single select statement and the values of multiple select statements.

HTML code:

<P> </p> <br/>
<Select id = "single">
<Option> Single </option>
<Option> Single2 </option>
</Select>
<Select id = "multiple" multiple = "multiple">
<Option selected = "selected"> Multiple </option>
<Option> Multiple2 </option>
<Option selected = "selected"> Multiple3 </option>
</Select>

JQuery code:

$ ("P"). append (
"<B> Single: </B>" + $ ("# single"). val () +
"<B> Multiple: </B>" + $ ("# multiple"). val (). join (",")
);

Result:

[<P> <B> Single: </B> Single <B> Multiple: </B> Multiple, Multiple3 </p>]

Get the value in the text box

HTML code:

<Input type = "text" value = "some text"/>

JQuery code:

$ ("Input"). val ();

Result:

Some text prior val (val) sets the value of each matching element. In jQuery 1.2, this can also assign the Set the value attribute of every matched element to the select element. in jQuery 1.2, this is also able to set the value of select elements, but selecting the appropriate options. return Value

JQuery

Parameters

Val(String): the value to be set.

Example

Set the value of the text box

HTML code:

<Input type = "text"/>

JQuery code:

$ ("Input"). val ("hello world! "); Values val (val) check, select, radio, and so on can all be used to return values

JQuery

Parameters

Val(Array <String>): the value used for check/select.

Example

Set the values of one select statement and multiple select statements.

HTML code:

<Select id = "single">
<Option> Single </option>
<Option> Single2 </option>
</Select>
<Select id = "multiple" multiple = "multiple">
<Option selected = "selected"> Multiple </option>
<Option> Multiple2 </option>
<Option selected = "selected"> Multiple3 </option>
</Select> <br/>
<Input type = "checkbox" value = "check1"/> check1
<Input type = "checkbox" value = "check2"/> check2
<Input type = "radio" value = "radio1"/> radio1
<Input type = "radio" value = "radio2"/> radio2

JQuery code:

$ ("# Single"). val ("Single2 ");
$ ("# Multiple"). val (["Multiple2", "Multiple3"]);
$ ("Input"). val (["check2", "radio1"]);

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.