Skip to content
Advertisement

Changing the type in IE with JavaScript

This code below works in all web browsers except IE:

JavaScript

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

if I don’t enter anything it will put the value back.

So I tried this:

JavaScript

as you can see the blur does not work.

Finally I got it, I needed to remove:

JavaScript

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
Advertisement