- This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
Viewing 4 posts - 1 through 4 (of 4 total)
- The forum ‘JavaScript’ is closed to new topics and replies.
The forums ran from 2008-2020 and are now closed and viewable here as an archive.
Home › Forums › JavaScript › [Solved] Changing Input Field Type
Hi, I have an Input field which the user will enter their password to. I set the field to type=”text” because the value is Password and they need to be able to read it. Then i have this code to check when the field is in focus,
$("#formPass").focus(function() {
$(this).css("background-color", "white");
$(this).css("color", "#333333");
var contents = $(this).val();
if(contents == "Password") {
$(this).attr("value", "");
$(this).attr("type", "password");
}
});
But is doesn’t work, the field empties when i click it but the type is still text rather than password dot form.
Any one know how to solve this?
Many Thanks
Chris
I believe that jQuery does not allow you to do this for security reasons.
However I know you can do this with pure javascript.
$("#formPass").focus(function() {
$(this).css("background-color", "white");
$(this).css("color", "#333333");
var contents = $(this).val();
if(contents == "Password") {
$(this).val("");
//$(this).attr("type", "password");
//
// Lets forget jQuery for a second.
var pass = document.getElementById('formPass');
pass.type = 'password';
}
});
Thankyou, Works Perfectly!
No problem.