Example of setting text values in jquery (setting text box DIV form values) _ jquery-js tutorial

Source: Internet
Author: User
This article describes how to set the content of jquery. The following example shows how to set the content through the text (), html (), and val () methods. For more information, see. Jquery settings-Text (), html (), and val ()

We will use three identical methods in the previous chapter to set the content:

Text ()-set or return the text content of the selected Element
Html ()-set or return the content of the selected element (including HTML tags)
Val ()-set or return the value of a form field

The following example shows how to set content through the text (), html (), and val () methods:

Instance

The Code is as follows:


$ ("# Btn1"). click (function (){
$ ("# Test1"). text ("Hello world! ");
});
$ ("# Btn2"). click (function (){
$ ("# Test2" 2.16.html ("Hello world!");
});
$ ("# Btn3"). click (function (){
$ ("# Test3"). val ("Dolly Duck ");
});

Text (), html (), and val () callback Functions

The preceding three jQuery methods are text (), html (), and val (), which also have callback functions. The callback function consists of two parameters: the subscript of the current element in the list of selected elements and the original (old) value. Then return the string you want to use with the new value of the function.

The following example demonstrates text () and html () with callback functions ():

Instance

The Code is as follows:


$ ("# Btn1"). click (function (){
$ ("# Test1"). text (function (I, origText ){
Return "Old text:" + origText + "New text: Hello world!
(Index: "+ I + ")";
});
});

$ ("# Btn2"). click (function (){
$ ("# Test2" functions .html (function (I, origText ){
Return "Old html:" + origText + "New html: HelloWorld!
(Index: "+ I + ")";
});
});

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.