This code below works in all web browsers except IE:
JavaScript
x
2
1
<input type="text" name="passwordLogin" value="Password" onfocus="if(this.value=='Password'){this.value=''; this.type='password'};" onblur="if(this.value==''){this.value='Password'; this.type='text'};" size="25" />
2
How can I fix it for IE?
I did some changes, but it still has an error.
I want it to work like this like here:
JavaScript
1
2
1
<input type="text" name="usernameLogin" value="Email" onfocus="if(this.value=='Email'){this.value=''};" onblur="if(this.value==''){this.value='Email'};" size="25" />
2
if I don’t enter anything it will put the value back.
So I tried this:
JavaScript
1
33
33
1
<td colspan="2" id="passwordLoginTd">
2
<input id="passwordLoginInput1" type="text" name="passwordLogin" value="Password" onfocus="passwordFocus()" size="25" />
3
<input id="passwordLoginInput2" style="display: none;" type="password" name="passwordLogin" value="" onblur="passwordBlur()" size="25" />
4
</td>
5
<script type="text/javascript">
6
//<![CDATA[
7
8
passwordElement1 = document.getElementById('passwordLoginInput1');
9
passwordElement2 = document.getElementById('passwordLoginInput2');
10
11
function passwordFocus() {
12
13
passwordElement1.style.display = "none";
14
passwordElement2.style.display = "inline";
15
passwordElement2.focus();
16
17
}
18
19
function passwordBlur() {
20
21
if(passwordElement2.value=='') {
22
23
passwordElement2.style.display = "none";
24
passwordElement1.style.display = "inline";
25
passwordElement1.focus();
26
27
}
28
29
}
30
31
//]]>
32
</script>
33
as you can see the blur does not work.
Finally I got it, I needed to remove:
JavaScript
1
2
1
passwordElement1.focus();
2
Advertisement
Answer
You cannot dynamically change a the type of an input element in Internet Explorer.
One possible workaround would be to:
- Dynamically create a new element.
- Copy the properties of the old element into the new element.
- Set the type of the new element to the new type.
- Then replace the old element with the new element.
You may want to check the following article for a JavaScript implantation of the above:
Another option would be to statically create the two elements, but having only one visible at a time. The visibility and the focus would depend on the value of the elements. In this case, you may want to use something like this:
JavaScript
1
16
16
1
<script type="text/javascript">
2
function checkType() {
3
var thisElement = document.getElementById('field_text');
4
var otherElement = document.getElementById('field_password');
5
6
if (thisElement.value === 'Password') {
7
otherElement.style.display = 'inline';
8
thisElement.style.display = 'none';
9
otherElement.focus();
10
}
11
}
12
</script>
13
14
<input type="text" value="Password" id="field_text" onfocus="checkType();" />
15
<input type="password" value="" style="display: none;" id="field_password" />
16