ggalan
05-22-2012, 04:10 AM
i have this jquery code that alters the input fields value.
i would like to control its 'type' attribute.
this kind of works but it feels off,
is there a better way of handling this task?
$('#password').live("click", function(){
if( $(this).val() == "Password" ) {
$(this).val("").css({'color':'#000'});
}else{
$(this).css({'color':'#000'});
}
var oldButton = $(this);
var newButton = oldButton.clone();
newButton.attr("type", "password");
newButton.attr("id", "newPassword");
newButton.insertBefore(oldButton);
oldButton.remove();
newButton.attr("id", "password");
});
$("#password").blur(function() {
if( $(this).val() == "" ) {
var oldButton = $(this);
var newButton = oldButton.clone();
newButton.attr("type", "text");
newButton.attr("id", "newPassword");
newButton.insertBefore(oldButton);
oldButton.remove();
newButton.attr("id", "password");
$(this).val("Password").css({'color':'#777'});
}
});
RE: this did the trick
$(this).get(0).type = 'password';
i would like to control its 'type' attribute.
this kind of works but it feels off,
is there a better way of handling this task?
$('#password').live("click", function(){
if( $(this).val() == "Password" ) {
$(this).val("").css({'color':'#000'});
}else{
$(this).css({'color':'#000'});
}
var oldButton = $(this);
var newButton = oldButton.clone();
newButton.attr("type", "password");
newButton.attr("id", "newPassword");
newButton.insertBefore(oldButton);
oldButton.remove();
newButton.attr("id", "password");
});
$("#password").blur(function() {
if( $(this).val() == "" ) {
var oldButton = $(this);
var newButton = oldButton.clone();
newButton.attr("type", "text");
newButton.attr("id", "newPassword");
newButton.insertBefore(oldButton);
oldButton.remove();
newButton.attr("id", "password");
$(this).val("Password").css({'color':'#777'});
}
});
RE: this did the trick
$(this).get(0).type = 'password';