Home > Web Front-end > JS Tutorial > body text

Use JS to dynamically change the input type attribute

亚连
Release: 2018-05-19 14:32:51
Original
1118 people have browsed it

This article mainly introduces the relevant information about Javascript dynamically changing the input type attribute, and attaches a simple example code. Friends in need can refer to it

Javascript dynamically changes the input type attribute:

Code implementation:

<script type="text/javascript">
  function shoppw(thebox){
    var ps = document.getElementById(&#39;ps&#39;);
    var pass = document.getElementById(&#39;pass&#39;);
    ps.removeChild(pass);
    var psImput = document.createElement("INPUT");
    
    if(pass.type == &#39;password&#39;){
      psImput.type = "text";
    } else {
      psImput.type = &#39;password&#39;;
    }
    psImput.id = &#39;pass&#39;;
    psImput.name = "password";
    psImput.maxlength="15";
    ps.appendChild(psImput);
  }
</script>
Copy after login

HTML code:

<td class="label"> * <label for="password" accesskey="">登录密码:</label></td>
  <td ><p id="ps"><input type="text" name="password" maxlength="15" id="pass" /></p>
   <input name="checkbox2" type="checkbox" value="true" checked="checked" id="show" onclick="shoppw(this)" />
   <label for="show" accesskey="">显示</label></td>
Copy after login

You can dynamically change the type attribute value of the input element at runtime

Above I compiled it for everyone. I hope it will be helpful to everyone in the future.

Related articles:

JS uses event delegation to add events to elements

Tips for using new() in JS

Summary and solution of common JS errors

The above is the detailed content of Use JS to dynamically change the input type attribute. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template