1

The following javascript doesnt work in IE9, but works fine in Firefox. Any idea how to solve this? Thx.

var element = e.srcElement || e.target;

if(element != null){
   if(element.tagName == "INPUT") {

   //alert("Before: " + element.getAttribute("type"));
   element.setAttribute("type", "password");
   //alert("After: " + element.getAttribute("type"));

  }
}
3
  • 3
    In what way does it not work? Commented Oct 31, 2012 at 22:12
  • See if element.type = 'password' works Commented Oct 31, 2012 at 22:15
  • The problem is the IE does not like changing the type attribute dynamically. Some solutions I've found are to replace the element with a newly created one with the same specs. Commented Oct 31, 2012 at 22:17

2 Answers 2

2

Since I've found that a plausible solution is to replace the element with a new one, you could use something like this:

var input = element;
var input2 = input.cloneNode(false);
input2.type = "password";
input.parentNode.replaceChild(input2, input);

Found here http://bytes.com/topic/javascript/answers/705445-dynamically-change-input-type-text-password and modified slightly. Another solution off there is to use createElement instead of cloning, but that might take some more work.

EDIT:

Forgot to mention that the problem seems to be that IE does not like changing the type attribute of an element dynamically after it has been included in the DOM.

Sign up to request clarification or add additional context in comments.

2 Comments

+1, but IE's problem is if the element is (or was) in a form. It's OK with changing type otherwise.
@RobG Hmm, really? Hadn't even bothered checking. For the few things I read, all people said was just the general "IE doesn't like changing its type dynamically". Good to know then. Do you have any links for this new form information? Just wanting to read about it. I'm sure I could find more if I looked more
2

Here are a few notes:

 // ------------v---favor e.target
var element = e.target || e.srcElement;

// -----v---simple truthy test
if (element){
  // ------------v---------v---- use nodeName, and toUpperCase() for safety
    if (element.nodeName.toUpperCase() == "INPUT") {

           // Not all IE respects the changing of the "type" on an input
        element.setAttribute("type", "password");

    } 
}

I think the tagName representation may have changed in IE9. Using nodeName should guarantee upper case characters, but there are a couple rare bugs to avoid, so we use toUpperCase().

In the past, IE has not allowed a change of the "type" of an input element. Not sire if that's still the case in IE9, but certainly IE8 and lower won't allow this.

9 Comments

(off topic, and without researching at all) Would you say using nodeName is the safe way to go, for legacy support and for moving forward, to get the "tag name"?
@Ian: nodeName gives the upper case tag name, plus it gives representations for different types of nodes, so it's generally considered a good choice to standardize on it. However there are a couple of IE bugs in a couple narrow cases, so the .toUpperCase() is advised.
Right, I would always use toUpperCase (or toLowerCase) when needing to compare to a string. But I guess I really meant - has nodeName always been supported? I guess as I look at MDN, it seems to be fully supported, so that was probably a silly concern. And I also see what you mean by what is returned for different types of nodes. Good to know. Not that I use this very often (or ever), but something I will remember to stick with.
@Ian: Yes, it has very good support. Just a couple IE 5.5 bugs. quirksmode.org/dom/w3c_core.html#t20 FWIW, quirksmode advises its use over tagName
BUGS IN IE 5.5? I CAN'T USE nodeName THEN. :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.