Home >Web Front-end >JS Tutorial >How to change the value of element object in jquery

How to change the value of element object in jquery

WBOY
WBOYOriginal
2021-11-23 18:06:114277browse

Jquery method to change the object value: 1. Use the "$(element)" statement to obtain the specified element object; 2. Use the val() method to change the value of the obtained element object. The syntax is "Element object. val(new value)".

How to change the value of element object in jquery

The operating environment of this tutorial: windows7 system, jquery1.10.0 version, Dell G3 computer.

How does jquery change the value of an element object

In jquery, you can use the val() method to change the value of the object. The val() method Returns or sets the value of the selected element. The value of an element is set via the value attribute. This method is mostly used for input elements. If this method does not set parameters, it returns the current value of the selected element.

The syntax of this method is:

$(selector).val(value)

Let’s take an example to see how to change the value of the object. The example is as follows:

1. Create a new html file , named test.html, is used to explain how jquery changes the value of input. Within the p tag, use the input tag to create a text input box and set the value to "default value". Set the id attribute of input to testinput, which is mainly used to obtain the input object through this id below.

Use the button tag to create a button with the button name "Change value". Bind the onclick click event to the button button. When the button is clicked, the setny() function is executed.

How to change the value of element object in jquery

#2. In the js tag, create the setny() function. Within the function, obtain the input object through id(testinput) and use the val() method to modify the input. value. For example, set value to "This is the new value".

How to change the value of element object in jquery

Open the test.html file in the browser and click the button to see the effect.

How to change the value of element object in jquery

After clicking the button:

How to change the value of element object in jquery

Summary:

1. Create a test.html file .

2. In the file, use the input tag to create a text input box, set the value to "default value", and create a button button to trigger the execution of the js function.

3. Create a function in the js tag. Within the function, obtain the input object and use the val() method to modify the value of the input.

Note:

The val() method can not only modify the value of the input, but also obtain the value of the input.

Recommended related video tutorials: jQuery video tutorial

The above is the detailed content of How to change the value of element object in jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn