1

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

3 Answers 3

2

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'));
Sign up to request clarification or add additional context in comments.

3 Comments

Follow up .. is it possible to add onclick .. and other events to newly created input element ?
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.
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.
2
newO.setAttribute('className' obj.getAttribute('class'));

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

4 Comments

well I read some articles .. just got confused .. anyways which is the solution , is it class or className?
Edited to give the actual answer.
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: using className that way works differently on different browsers; see my comment on David Hedlund's answer: stackoverflow.com/questions/1907006/…
0

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.

2 Comments

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
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).

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.