Search code examples
javascriptbuttonhtml-selectdisabled-input

Disable button from option select


Hello i want to disable button when a select option is a specific attribute.

My code is this:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script type="text/javascript">
	$("company").on('change',function(){
	 if($(this).find(':value').text()=="Dove Sei?")
	  $("#buttonSurvey").attr('disabled',true)
	 else
		$("#buttonSurvey").attr('disabled',false)
	});
</script>
<select class="selectpicker" data-live-search="true" id="company" data-size="5" title="Dove Sei?" data-width="100%">
  <option data-tokens="Dove sei?" value="Dove Sei?" id="Dove Sei?" selected>Dove sei?</option> 
  <option data-tokens="another" id="another" value="another" data-picture="another.png">another</option>
  <option data-tokens="another" id="another" value="another" data-picture="another.png">another</option>
  <option data-tokens="another" id="another" value="another" data-picture="another.png">another</option>
</select>



<ul class="list-group" style="text-align: center;">
 <li class="list-group-item" style="border-style: none;"><h4>Servizio</h4>
<div class="btn-group">
	<button id="buttonSurvey" type="submit" class="btn btn-default" data-toggle="modal" data-target="#voteReg" name="survey1" value="green" onclick="getVote(this.value)"  style="border: none;"><img src="Q1.png}" class="Vote"></button>
	<button id="buttonSurvey" type="submit" class="btn btn-default" data-toggle="modal" data-target="#voteReg" name="survey1" value="green" onclick="getVote(this.value)"  style="border: none;"><img src="Q2.png}" class="Vote"></button>
 </div></li>

If option value is Dove Sei? i want disable the button. I can try this code but don't run.

Can you help me?


Solution

    1. Your $("company") selector does not match anything, if you want to select by id, you should have used $("#company").
    2. Instead of $(this).find(':value').text(), please check: How do I get the text value of a selected option? - Since you also set the value property, you could simply use $(this).val()
    3. You have two buttons with the same id, keep in mind that the $("#buttonSurvey") selector in your action handler will always and only select the first one.