I'm having a strange issue where I'm unable to change the displayed text of a button using jQuery. The idea is to be able to toggle a field between password<=>text, with an associated button whose text switches between 'Show' and 'Hide'.
This is the button:
<input type="button" id="login-form-showchars" value="Show" onclick="showCharsClicked();"/>
And this is the showCharsClicked function:
function showCharsClicked() {
var passwordField = document.getElementById("login-form-password");
if (passwordField.type == "password") {
passwordField.type = "text";
//TODO: change text of button to 'Hide'
} //if
else {
passwordField.type = "password";
//TODO: change text of button to 'Show'
} //else
passwordField.value = value;
} //showCharsClicked
The function works, but no matter what code I insert into the TODO areas, the text on the button remains unchanged. I have tried ALL of the following with both 'input' and 'button' types:
document.getElementById('login-form-showchars').value = 'Hide';
$("#login-form-showchars").prop('value', 'Hide');
$("#login-form-showchars").html('Hide');
$("#login-form-showchars").attr('value', 'Hide');
$("#login-form-showchars").val('Hide');
$("#login-form-showchars").text('Hide');
Does anyone know what might be going wrong? By inspecting the button I see that its value IS changing, but this change is never reflected visually.
Thanks!
====================================
EDIT: I found a way to get it working and thought I would record it here for posterity. Thanks for all of your answers, I'm thinking now that the problem was caused because of the specific jQuery interface my program uses; that's why it worked for so many other people but not for me.
The problem was that the program created some nested 'span' tags underneath the button in runtime, so that during the program's execution it looked like this:
<a data-role="button" href="#" id="login-form-showchars" data-theme="v" class="ui-btn ui-btn-corner-all ui-shadow ui-btn-up-v" data-transition="pop" data-direction="reverse">
<span class="ui-btn-inner ui-btn-corner-all">
<span class="ui-btn-text">Hide</span>
</span>
</a>
While I was trying to edit the button's value, what really needed changing was the text inside the button's 'span class="ui-btn-text"' tag.
I changed the button declaration to:
<a data-role="button" href="#" id="login-form-showchars">Show</a>
And the command inside the function to:
$("#login-form-showchars .ui-btn-text").text("Hide");
And this worked fine. Hope this helps someone not have to go through what I did!
valuevariable you assign topasswordField.value? Then, there is no need to change the value ofpasswordFieldto show/hide the password.