Change type attribute of Input Element With JavaScript

In this tutorial we will see How To Change type attribute of Input Element With JavaScript. HTML DOM type Property can be used to change the value of type attribute of input element.

HTML Code

HTML Code is given below, in this code we have one HTML input element and seven buttons, each to change the type of input element.

<input type="text" id="input">
<br><br>    
<button onclick="pass()">Password Type</button>
<button onclick="number()">Number Type</button> 
<button onclick="email()">Email Type</button>
<button onclick="file()">File Type</button>
<button onclick="radio()">Radio Type</button>
<button onclick="color()">Color Type</button>
<button onclick="date()">Date Type</button> 

JavaScript Code

Take a look at the JavaScript code, the HTML Dom type property is used to change the type attribute of input element.

onclick event is used to trigger the seven different functions, for each button, to change type of input element to seven different types.

<script>
function pass()
{
document.getElementById('input').type='password';
}
function number()
{
document.getElementById('input').type='number';
}
function email()
{
document.getElementById('input').type='email';
}
function file()
{
document.getElementById('input').type='file';
}
function radio()
{
document.getElementById('input').type='radio';
}
function color()
{
document.getElementById('input').type='color';
}
function date()
{
document.getElementById('input').type='date';
}
</script>

HTML DOM type Property

.type Property sets or returns the type of the input element.

Types of Input Element

Different types of input elements are listed below, we have used just seven in this example.

<input type="text">
<input type="email">
<input type="hidden">
<input type="file">
<input type="button">
<input type="checkbox">
<input type="color">
<input type="date">
<input type="submit">
<input type="image">
<input type="datetime-local">
<input type="month">
<input type="number">
<input type="password">
<input type="radio">
<input type="range">
<input type="reset">
<input type="search">
<input type="tel">
<input type="time">
<input type="url">
<input type="week">

Demo

Video Tutorial

Watch video tutorial on Change type attribute of Input Element With JavaScript.

How to Change Image on Hover with JavaScript How to Disable Button by Class Name in JavaScript How To Change Image Size with JavaScript How to change Image opacity with JavaScript How to Change image src on click with JavaScript How to get the lang attribute value in JavaScript How to Get attribute Value using JavaScript How To Check if Attribute Exists or not with JavaScript How To Count number of links on Page with JavaScript How To Scroll Page to Bottom using JavaScript How To Detect Shift Key Press with JavaScript Change Text Color On Hover with JavaScript Hide and Show div using JavaScript Get Button text with JavaScript Get textarea value with JavaScript Get table row Height with JavaScript Auto Increase width of input field with JavaScript Set Textarea maxLength with JavaScript Set Textarea Value with JavaScript JavaScript Count list items JavaScript set input field value Count Button Clicks with JavaScript Scroll Page to the Top on Click with JavaScript Change id of Element with JavaScript JavaScript Change li Text Color