Hi Guys,
I’m working on masking some input fields, for the credit card field requirement is that characters should be masked while you are typing in the field and also on focus out. So ofcourse I achieved that easily by converting the field to type password. But the next requirement is that when the user clicks back in the field (focus), it should should show the value but as soon as the user starts typing, the value should be masked. I’ve tried all sorts of things but I was finally stuck because of IE 8 , as it won’t let me change the type. Does anyone have a solution that will work in all browsers including IE 8?
jScript (IE8 and earlier) doesn’t allow you to change the type of an input after you create it.
As you are already using jQuery the jQuery solution already provided is the simplest solution.
For anyone who has this problem who isn’t using jQuery see http://javascriptexample.net/domform08.php for how to do it with plain J(ava)Script (JScript for IE8 and earlier and JavaScript for modern browsers)
Thanks for your replies, this didn’t work in IE8, I’m also using an older version of jQuery 1.4.2, right now I don’t have the flexibility to update it for the application. So sorry guys but none of the above have worked for me so far
Thanks for all your help so far guys, if the above is true then below should work, but it doesn’t . I have not even checked this in IE yet, only tested in chrome and it’s not working. So what do you think I’m missing? There are no errors in the console but just absolutely nothing happens when I type in the filed, it just acts as text field.
var keyup = false;
$('form').delegate('blur', 'input[type=text]', function() {
var $this = $(this),
val = $this.val(),
$pass = $('<input class="pwd" type="password" />');
$pass.insertAfter(this);
$this.remove();
if (keyup) $pass.focus();
keyup = false;
$pass.val(val).addClass('ready');
}).delegate('keyup', 'input[type=text]', function() {
keyup = true;
$(this).blur();
}).delegate('focus', 'input[type=password].ready', function() {
var $this = $(this),
val = $this.val(),
$txt = $('<input type="text" />');
$txt.insertAfter(this);
$this.remove();
$txt.focus().val(val);
});
})();
Yes, your solution does not work in IE, don’t think it even does in IE9, haven’t checked 10 or 11.
I’m not sure what is wrong but I cannot get delegate to work, simple piece of code as below won’t even work. Nothing triggers, I’m not sure what is going on, possible you can check David’s solution on your Machine now that you have IE8? Appreciate the help!
$(document).ready(function(){
var keyup = false;
$('form').delegate('click', 'input[type=text]', function(){
alert('test');
});
IE8 only runs JScript ands so only supports attachEvent and not addEventListener.
I already posted a link to a plain JavaScript version that does work even in IE5 back in post 4 of this thread.
JScript does not support changing the type attribute. It does support conditional comments so that you can use those to readily distinguish between browsers that run JavaScript and those that run JScript (IE9 slightly complicates things by supporting both).
omg sorry I thought I had copy/pasted the new code you provided but I was still using the old code. Now that I have the code updated it is somewhat working in IE 8, sometimes it has a weird behavior, the cursor jumps one character behind while you are typing, so messing up the input. In IE 9, it does not switch to password type at all while you are typing and also the cursor jumps back one character every time a character is typed in. I feel we’re almost there! I’ll keep looking into it, let me know if you any suggestions for this.
Yeah, sorry, I used a pollyfill for this. I don’t this wasn’t the problem (although anything is possible).
About that, I couldn’t get it to work on the latest Chrome on Linux.
I copied the source code verbatim from your page, but I get the following error when I try and run it: