tags:

views:

87

answers:

3

Hello everybody I'm trying to replace the 'text' input type to 'password' . And it works with following code :

function replaceT(obj){
      var newO=document.createElement('input');
      newO.setAttribute('type','password');
      newO.setAttribute('name',obj.getAttribute('name'));
      obj.parentNode.replaceChild(newO,obj);
      newO.focus();
      }

But I want to add class of my previous input to the new input and I tried something like this :

function replaceT(obj){
      var newO=document.createElement('input');
      newO.setAttribute('type','password');
      newO.setAttribute('className' obj.getAttribute('class'));
      newO.setAttribute('name',obj.getAttribute('name'));
      obj.parentNode.replaceChild(newO,obj);
      newO.focus();
      }

What am I doing wrong, or if that is not possible, how to set the new class manually .. Thank you

+2  A: 
newO.setAttribute('className' obj.getAttribute('class'));

You are using 'className' for setting the attribute, but 'class' for getting it. Both must be 'className'.

Serhat Özgel
well I read some articles .. just got confused .. anyways which is the solution , is it class or className?
c0mrade
Edited to give the actual answer.
Serhat Özgel
not true, it doesn't work
c0mrade
I tried it and it definitely works. May be it does not work for you because you are missing the comma between the two parameters?
Serhat Özgel
@Serhat: using `className` that way works differently on different browsers; see my comment on David Hedlund's answer: http://stackoverflow.com/questions/1907006/replace-input-with-javascript/1907028#1907028
NickFitz
+2  A: 

This should work across all browsers:

newO.className = obj.className;

I'm not sure whether you should use className or class in setAttribute, but whatever it is, it is definitely the same as in getAttribute, so this is definitely wrong:

newO.setAttribute('className' obj.getAttribute('class'));
David Hedlund
This totally works .. thank you
c0mrade
Follow up .. is it possible to add onclick .. and other events to newly created input element ?
c0mrade
well yes, it is definitely possible. for starters, you'd want to look at *attachEvent* for IE and *addEventListener* for all others, but for absolute compatibility you really want to look into a framework, such as jQuery, for this.
David Hedlund
Just to expand on this: `class` *should* work for `setAttribute` and `getAttribute` but, of course, IE gets it wrong (except IE8 in IE8 mode), and I believe other browsers have had to do IE's thing as well as the right thing for compatibility reasons :-( David's method using the `className` property will work everywhere.
NickFitz
A: 

Sorry for going in the other direction, but why do you replace the element instead of just changing its type in the first place? Than you won't need to worry about adding the same className to the new element.

Just being curious.

Mario Mikić
Because I want to add label to the field "Password", if I change it at first to type password then it will be masked .. razumijes :D
c0mrade
lol :) razumijem ;)
Mario Mikić
IE (6 and 7, not tried in 8) will throw an exception if you try to change the `type` of an `input` element after it has been added to the document (even if you try to remove it, change it, then re-add it).
NickFitz