Home >Web Front-end >Front-end Q&A >How to modify the type attribute in jquery

How to modify the type attribute in jquery

青灯夜游
青灯夜游Original
2022-03-01 18:20:543788browse

In jquery, you can use the attr() method to modify the type attribute of the element. This method can set the attribute value of the selected element. The syntax is "$(selector).attr("type","new Value ")" or "$(selector).attr({type:new value})".

How to modify the type attribute in jquery

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

In jquery, by obtaining the input object, the type can be changed using the attr() method. Syntax:

$(selector).attr("type","新值");
$(selector).attr({type:新值});

Example:

In the p tag, use the input tag to create an input box and set its value default value to abcdefg.

How to modify the type attribute in jquery

In the test.html file, set the id attribute value of the input tag to tuinput, which is mainly used to obtain the input object through this id below.

How to modify the type attribute in jquery

In the test.html file, use the button tag to create a button with the button name "Change the type attribute of the input".

How to modify the type attribute in jquery

In the test.html file, bind the onclick click event to the button button. When the button is clicked, the editype() function is executed.

How to modify the type attribute in jquery

In the js tag, create the editype() function. Within the function, obtain the input object through id(tuinput), and use the attr() method to change the type attribute value to password. .

How to modify the type attribute in jquery

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

How to modify the type attribute in jquery

How to modify the type attribute in jquery

[Recommended learning: jQuery video tutorial, web front-end video

The above is the detailed content of How to modify the type attribute 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