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