views:

64

answers:

1

Hi all, I already asked a question today : This one

Now I have a code that works, but works only in FF(fully) and partially in IEs(7 and 8).

function replaceT(obj){
     var newO=document.createElement('input');
     newO.className = obj.className;
     newO.style.width = '118px';
     newO.style.height = '17px';
     newO.style.color = '#666';
     newO.setAttribute('maxlength','30');
     newO.setAttribute('type','password');
     newO.setAttribute('onblur','this.value=\'Password\'; this.type=\'text\'');
     newO.setAttribute('onfocus','this.value=\'\'; this.type=\'password\'');
     newO.setAttribute('tabindex','2');
     newO.setAttribute('value','');
     newO.setAttribute('id','password_property_input');
     newO.setAttribute('name',obj.getAttribute('name'));
     obj.parentNode.replaceChild(newO,obj);
     newO.focus();
}

In firefox everything works perfect, I call this function on focus so my old type password input becomes type text input.

Now when I click outside of this input(onblur) I get the field type changes to text again and the value restores to Password. And if the field is clicked again(onfocus) my text type input again becomes password and the value is back to ''.

This works perfectly in FF, but none of this works in IE, when I say none I mean onblur and onfocus. The first change from text to password input works. Thank you for any answers

I found even better solution, but it also doesn't work in IE :

Inside window.load function put :

applyPasswordType(document.getElementById('password_property_input'), 'Password', 'text');

And the function itself ..

function applyPasswordType(elem, val, typ) {
      elem.value = val;
      elem.type = typ;
      elem.onfocus = function() {
        if(this.value == val) {
          this.style.color = '';
          this.type = 'password'; //If in focus, input type will be 'password'
          this.value = '';
        }
      }

      elem.onblur = function() {
         if(this.value == '') {
           this.value = val;
           this.type = 'text'; //On blur, input type will be 'text' in order to show value
         }
       }
     }

I'm still trying to figure out how to fix this with addeventlistener ..

+1  A: 

setAttribute is broken in IE, don't use it.

Use properties directly:

foo.onevent = function () { ... }

Better yet, use addEventListener/attachEvent

You might also find that IE can't change the type of an existing input. If so, you'll have to create a new input and replace the existing one.

David Dorward
what should I use ?
c0mrade
So I should replace the input every time in IE? is there a way arround it? I mean another approach towards the whole big picture of what I'm trying to do. I want my password to have value 'Password' and when focused to change to real password field with password masking, also onblur to restore the password value of password field.
c0mrade
Also could you clarify the "foo.onevent = function () { ... }" part i have no clue what you are speaking of (my fault). Thank you
c0mrade
I could clarify the onevent = stuff ... but the linked article about addEventListner/attachEvent is a better solution.
David Dorward
As for a better solution, if I take a step back and look at the problem - stop abusing the value attribute as a label. Use `<label>` elements for labels. (If you really want to be clever, then play around with putting the label underneath the input. See http://dorward.me.uk/tmp/label-work/example.html for an **unfinished** example)
David Dorward
+1, hit both nails on the head. @c0mrade: re `setAttribute` and events, David is referring to http://webbugtrack.blogspot.com/2007/08/bug-242-setattribute-doesnt-always-work.html As for not being able to change the type of an existing input, David is referring to http://www.universalwebservices.net/web-programming-resources/javascript/change-input-element-type-using-javascript
Crescent Fresh
Ok, I got it .. "on*: Inline events can not be set in IE, attach event handlers instead" how should I use them instead .. I got this link quirksmode.org/js/events_advanced.html from David, I'm not sure how to fix this .. not really sure what to do now, I am not really advanced javascript guy .. if you know what I mean .. and I even had to google "hit both nails on the head"; to find out the meaning of that as well
c0mrade
@c0mrade: David's link is about using `addEventListener` and `attachEvent` for attaching event handlers, but to keep it simple and get it working you can stick to `onfocus=` and `onblur=` just fine. The real problem is changing the `type` attribute in IE. See my prior link.
Crescent Fresh