Reputation: 569
OK, so I have a button that needs to be disabled. I wrote a JavaScript function that is meant to detect if a field is blank/null, and if it is disable the save button next to a field. It would appear to be working as far as detecting the blank field, and throwing the alert, but no matter what I try as far as setting the button to disable. I cannot get it to do so.
Here is what I'm currently trying...
function validateBlank(){
var x = document.forms["FuForm"]["Field1"].value;
if (x=null || x == "") {
alert("Must Be Between 1 and 300 Characters");
document.getElementById("saveTest").setAttribute("disabled", "disabled");
return false;
}
}
I've also tried this variant...
document.getElementById("saveTest").disabled
and the button/link is setup like this.
<a href="#">
<img id="saveButton" src="${contextPath}/img/icon_sav.gif" name="saveTest" " alt="Disk icon. Save change" title="Save change"/>
</a>
Upvotes: 1
Views: 3360
Reputation: 850
The correct way is:
document.getElementById("saveTest").disabled = true;
Source: http://www.w3schools.com/jsref/prop_pushbutton_disabled.asp
Hope this helps :)
EDIT: your button is in fact an image, and an image cannot be disabled in this way! To have an image button that can be disabled, the correct HTML syntax should be in this form:
<input type="image" src="someimage.png" height="20px" width="20px" id="saveTest" name="button" />
i.e. an input of type image .
Upvotes: 2