Page 1 of 1

I just want to set an attribute IE...

Posted: Wed Sep 17, 2008 5:00 pm
by JellyFish
First I tried:

Code: Select all

 
element.type = "text";
 
Then I tried:

Code: Select all

 
element.attributes['type'].value = "text";
 
and then finally:

Code: Select all

 
element.setAttribute('type', 'text');
 
and none of them worked. "Please IE, don't do this to me," I said. IE replied, "This command is not supported.".

I'm trying to change the type attribute to a input element. How does IE want me to do this? What's the work around? :banghead:

Re: I just want to set an attribute IE...

Posted: Wed Sep 17, 2008 9:26 pm
by JAB Creations
Maybe you're looking for setAttributeNode?

Re: I just want to set an attribute IE...

Posted: Thu Sep 18, 2008 12:52 am
by JellyFish
Thanks for your reply John.

I've looked into setAttributeNode, and try this in IE:

Code: Select all

 
var attr =  document.createAttribute("type"); 
attr.value = "password";
document.forms[0].q.setAttributeNode(attr);
 
and I get the same results in IE—command is not supported. Weird, strange and awkward. Anyway I'll look into it more tomorrow, I'm tired..

Re: I just want to set an attribute IE...

Posted: Thu Sep 18, 2008 3:05 am
by JAB Creations
MSDN documentation...not much though it may help...
http://msdn.microsoft.com/en-us/library ... S.85).aspx

Also remember that it's simple to do DOM detection...

Code: Select all

if (!setAttributeNode) {alert('the setAttributeNode object is not supported');}
Looks like (from some very casual searching) that setAttributeNode may be read-only in some circumstances. You may have to do something to get around it though I'm not really sure.

Do the searching when you can and if you need help testing I'll try to help...I've been getting much better at JavaScript of late. :wink:

Re: I just want to set an attribute IE...

Posted: Fri Sep 19, 2008 2:31 pm
by JellyFish
I figured out a solution. It involves recreating an input element and replacing the existing element with it, also I used jQuery.

Code: Select all

 
    function passwordFocusFunction()
    {
        if (this.value == "Enter Password")
        {
            var newInput = document.createElement("input");
            newInput.id = "login-password";
            newInput.type = "password";
            $(this).replaceWith(newInput);
            
            $("#login-password").focus().blur(function()
            {
                if (this.value == "")
                {
                    var newInput = document.createElement("input");
                    newInput.id = "login-password";
                    newInput.type = "text";
                    $("#login-password").replaceWith(newInput);
                    $("#login-password").val("Enter Password");
                }
                
                $("#login-password").focus(passwordFocusFunction);
            });
        }
        
    }
    $("#login-password").focus(passwordFocusFunction);
 
I'd say it's a lot for something that could be so simple: element.setAttribute("type", "password"). But this works and I'd figure I'd post it here for others convenience.

If you'd like you could even give tips on how to make it better. :D

Re: I just want to set an attribute IE...

Posted: Fri Sep 19, 2008 7:38 pm
by JAB Creations
Mentioning and relying on jQuery in every JavaScript thread makes me wonder why I even bother in the client side forum. :nono:

Re: I just want to set an attribute IE...

Posted: Tue Sep 23, 2008 12:54 am
by JellyFish
JAB Creations wrote:Mentioning and relying on jQuery in every JavaScript thread makes me wonder why I even bother in the client side forum. :nono:
Hmm, I'm not sure I understand what you mean?

Re: I just want to set an attribute IE...

Posted: Tue Sep 23, 2008 1:32 am
by JAB Creations
It means I'm getting very annoyed with the constant reliance on jQuery. Most times people are recommending it for very simple tasks. There is absolutely no reason why 99% of the pages on the internet (that aren't gallery pages) should exceed 50-60KB if codded correctly.

Re: I just want to set an attribute IE...

Posted: Wed Sep 24, 2008 4:12 pm
by kaszu
Instead of replacing text input with password input and then back again, you can have both and just show/hide text and password inputs when needed. At the end anyway you only need password inputs value, so text can have any 'name' and there won't be a conflict.

About 50-60kb: jQuery minified and gziped takes 16kb, and if proper headers are used, then it's downloaded only once.
Reason why people (at least me) is using libraries is that they reduce amount of possible bugs, increases maintainability and allows to reduce amount of code needed to do a task. I believe that's good enough reason to use the library as long as it's not used only to do one simple task, which could be solved otherwise.

Re: I just want to set an attribute IE...

Posted: Thu Sep 25, 2008 9:53 am
by pickle
I'm not sure it's possible to change the type of an input element. It doesn't make sense that you could turn a radio button into a textarea for example. I can also see some security issues in being able to convert a password field into a plaintext element.