Skip to content Skip to sidebar Skip to footer

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.

function enableButton()
{
    var selectelem = document.getElementById('corpusname');
    var btnelem = document.getElementById('seedoc');
    btnelem.disabled = !selectelem.value;
}
<select class='corpusname' id='corpusname' size='1' name='corpusname' required onchange="enableButton()"/>
<option value=''>Select a corpus</option>
<option value="1">1</option>
</select>
<input type="button" id="seedoc" disabled value="Submit">

Solution 2:

Here is a little snippet without the php part, the php part is still the same.

function ValidateDropDwon(dd){
  var input = document.getElementById('seedoc')
  if(dd.value == '') input.disabled = true; else input.disabled = false;
}
<select class='corpusname' id='corpusname' size='1' name='corpusname' required onchange="ValidateDropDwon(this)">
     <option value=''>Select a corpus</option>
     <option value='test'>test</option>
</select>

<input type='submit' id='seedoc' class='seedoc' disabled value='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") or die(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()

<script type="text/javascript">
    function enableButton(){
      $("#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"