I just want to set an attribute IE...

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

I just want to set an attribute IE...

Post 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:
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

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

Post by JAB Creations »

Maybe you're looking for setAttributeNode?
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

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

Post 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..
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

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

Post 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:
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

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

Post 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
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

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

Post 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:
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

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

Post 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?
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

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

Post 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.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

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

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply