Home >Web Front-end >JS Tutorial >How to change the value of element object in jquery
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)".
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.
#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".
Open the test.html file in the browser and click the button to see the effect.
After clicking the button:
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!