how can i change the text of a button when clicked? and change it back when clicked again? i have this javascript i made.and its not giving me what i want.please help?
$('#addbtn').click(function (){
if(document.getElementById('addMat').style.display == "none"){
document.getElementById('addMat').style.display= "inline";
$(this).val('Cancel');
}else
document.getElementById('addMat').style.display= "none";
$(this).val('Add Material');
});
Just do it all with jQuery syntax:
$('#addbtn').click(function (){
var addMat = $('#addMat');
if( addMat.css('display') === 'none' ) {
addMat.css('display','inline');
$(this).val('Cancel');
} else {
addMat.css('display','none');
$(this).val('Add Material');
}
});
$('#addbtn').click(function (){
if(document.getElementById('addMat').style.display === "none"){
document.getElementById('addMat').style.display = "inline";
$(this).attr("value","Cancel");
}else{
document.getElementById('addMat').style.display = "none";
$(this).attr("value","Add Material");
}
});
or Better...
$('#addbtn').click(function (){
if( $('#addMat').css('display') === 'none' ){
$('#addMat').css('display','inline');
$(this).attr("value","Cancel");
} else {
$('#addMat').css('display','none');
$(this).attr("value","Add Material");
}
});
$(this).prop('value', 'Cancel');
or
$(this).html('Cancel');
The Best way to do it is create a button and put a span tag around the text
$("span", this).text("My NEW Text");