2

I have dropdown list with collection options. When option with certain class is selected I want submit button to be disabled. This is what I've tried to do:

<form id="addToCollection">
   <select id="add_to_collection">
       <option value="1" class="highlight">1</option>
       <option value="2">1</option>
       <option value="3">3</option>
   </select>
    <input  id="add-object-to-collection"
    type="submit" value="Add" class="button"/>
</form>
<input  id="add-object-to-collection" type="submit" value="Add" class="button"/>
if($('#add_to_collection option').hasClass('highlight')) {
  $("#add-object-to-collection").prop('disabled', true);
  $("#add-object-to-collection").attr('value', 'Added');
} else {
  $("#add-object-to-collection").prop('disabled', false);
  $("#add-object-to-collection").attr('value', 'Add');
}

So here, when option 1 is selected I want button to be disabled and when other options are selected button be enabled. However here button is disabled for all options.

2 Answers 2

1

Try this

$('#add_to_collection').on('change', function () {
  var $option = $('option:selected', this);
  var $button = $('#add-object-to-collection');
  
  if ($option.hasClass('highlight')) {
    $button.prop('disabled', true).val('Added');
  } else {
    $button.prop('disabled', false).val('Add');
  }
}).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form id="addToCollection">
  <select id="add_to_collection">
    <option value="1" class="highlight">1</option>
    <option value="2">1</option>
    <option value="3">3</option>
  </select>
  
  <input  id="add-object-to-collection" type="submit" value="Add" class="button" />
</form>

Sign up to request clarification or add additional context in comments.

Comments

1

Your code is almost working. The problem in your case is that you aren't checking to see if the selected option element has a class of highlight. You could use the :selected selector in order to select the selected option element. In addition, you would also need to wrap that logic within a change event callback.

You could simplify the code to the following:

This essentially listens to the change event, and determines whether the selected option element has the class highlight. The boolean is used to disabled/enable the corresponding button.

I also chained a .change() event so that the event would be fired initially.

Example Here

$('#add_to_collection').on('change', function () {
   var hasClass = $(this).find(':selected').hasClass('highlight');
   $('#add-object-to-collection').prop('disabled', hasClass).val(hasClass ? 'Added' : 'Add');
}).change();

$('#add_to_collection').on('change', function () {
   var hasClass = $(this).find(':selected').hasClass('highlight');
   $('#add-object-to-collection').prop('disabled', hasClass).val(hasClass ? 'Added' : 'Add');
}).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="addToCollection">
    <select id="add_to_collection">
        <option value="1" class="highlight">1</option>
        <option value="2">1</option>
        <option value="3">3</option>
    </select>
    <input id="add-object-to-collection" type="submit" value="Add" class="button" />
</form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.