Disable Button When No Option Is Selected From Dropdown
I have an input type button (it can not be submit type because it triggers a secondary action inside a form) that I need to maintain disabled if no selection made on a previous dro
Solution 1:
Just add an onchange
event to the select
.
functionenableButton()
{
var selectelem = document.getElementById('corpusname');
var btnelem = document.getElementById('seedoc');
btnelem.disabled = !selectelem.value;
}
<selectclass='corpusname'id='corpusname'size='1'name='corpusname'requiredonchange="enableButton()"/><optionvalue=''>Select a corpus</option><optionvalue="1">1</option></select><inputtype="button"id="seedoc"disabledvalue="Submit">
Solution 2:
Here is a little snippet without the php part, the php part is still the same.
functionValidateDropDwon(dd){
var input = document.getElementById('seedoc')
if(dd.value == '') input.disabled = true; else input.disabled = false;
}
<selectclass='corpusname'id='corpusname'size='1'name='corpusname'requiredonchange="ValidateDropDwon(this)"><optionvalue=''>Select a corpus</option><optionvalue='test'>test</option></select><inputtype='submit'id='seedoc'class='seedoc'disabledvalue='See doc' />
Solution 3:
since you list jQuery
I will use that here.
echo"<select class='corpusname' id='corpusname' size='1'
name='corpusname' required onChange='enableButton()' />
<option value=''>Select a corpus</option>";
// This query gives the other options from a database$result = mysqli_query($db, "SELECT * FROM corpus_info") ordie(mysqli_error($db));
while($cpsmlg = mysqli_fetch_array($result)){
echo"<option value='".$cpsmlg['corpus']."'>".$cpsmlg['title']."</option>";
}
echo"</select>
<a id='theLink' target='_blank'>
// This is the button to be disabled
<input type='button' id='seedoc' class='seedoc' disabled value='See doc' /></a>";
then some javascript on the page to accomplish the onchange()
<scripttype="text/javascript">functionenableButton(){
$("#seedoc").prop('disabled', false);
}
</script>
I would put in a check, if corpusname = ''...
but it will not ever be changed back to a ''
, so onChange()
should suffice.
Solution 4:
If You have Jquery Included , You Can use this (EASIEST) :
$('#corpusname').on("change",function(){
if($(this).val() == ''){
$('#seedoc').attr('disabled','disabled'); //Disables if Values of Select Empty
}else{
$('#seedoc').removeAttr('disabled');
}
});
Post a Comment for "Disable Button When No Option Is Selected From Dropdown"