$(document).ready(function() {
// #login-box password field
$('#password').attr('type', 'text');
$('#password').val('Password');
});
This is supposed to change the #password
input field (with id="password"
) that is of type
password
to a normal text field, and then fill in the text “Password”.
It doesn’t work, though. Why?
Here is the form:
<form enctype="application/x-www-form-urlencoded" method="post" action="/auth/sign-in">
<ol>
<li>
<div class="element">
<input type="text" name="username" id="username" value="Prihlasovacie meno" class="input-text" />
</div>
</li>
<li>
<div class="element">
<input type="password" name="password" id="password" value="" class="input-text" />
</div>
</li>
<li class="button">
<div class="button">
<input type="submit" name="sign_in" id="sign_in" value="Prihlásiť" class="input-submit" />
</div>
</li>
</ol>
</form>
This Worked for me.
One step solution
heres a DOM solution
An ultimate way to use jQuery:
Leave the original input field hidden from the screen.
Create new input field on the fly by JavaScript.
Migrate the ID and value from hidden input field to the new input field.
To get around the error on IE like
type property cannot be changed
, you may find this useful as belows:Attach click/focus/change event to new input element, in order to trigger the same event on hidden input.
Old hidden input element is still inside the DOM so will react with the event triggered by new input element. As ID is swapped, new input element will act like the old one and respond to any function call to old hidden input's ID, but looks different.
It's very likely this action is prevented as part of the browser's security model.
Edit: indeed, testing right now in Safari, I get the error
type property cannot be changed
.Edit 2: that seems to be an error straight out of jQuery. Using the following straight DOM code works just fine:
Edit 3: Straight from the jQuery source, this seems to be related to IE (and could either be a bug or part of their security model, but jQuery isn't specific):
I've created a jQuery extension to toggle between text and password. Works in IE8 (probably 6&7 as well, but not tested) and won't lose your value or attributes:
Works like a charm. You can simply call
$('#element').togglePassword();
to switch between the two or give an option to 'force' the action based on something else (like a checkbox):$('#element').togglePassword($checkbox.prop('checked'));