1

I have select box what i want if there is style="color:#cccccc" in option then disable button show else enable button show.

New Note : i want to add disable attribute to button if there is style color attribute in option else button keep enable if there is no style color attribute in option

<select class="selected" >
    <option>Bank 1</option>
    <option style="color:#cccccc">Bank 2</option>
    <option>Bank 3</option>
</select>

<button class="btn_enabled btn">Pay Enabled</button>
<button class="btn_disabled btn" disabled>Pay Disabled</button>
$(function () {
  $(".selected").on("change", function () { 
      "style" === $(this).attr() 
          ? $(".btn_enabled").show() 
          : $(".btn_enabled").hide() 
  });
});
.btn_disabled { display: none; }

5 Answers 5

2
$(function () {
$(".selected").on("change", function () { 
     $(".btn_enabled").show( $('option:selected', this).attr('style') );
});

sources: https://learn.jquery.com/using-jquery-core/faq/how-do-i-get-the-text-value-of-a-selected-option/

jQuery - getting custom attribute from selected option

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

1 Comment

This is the minimal solution.
1

You have to capture the selected option. Also better to use a select option as first option

$(function () {
    $(".selected").on("change", function () { 
    if($(this).find(':selected').attr('style')) {
      $(".btn_enabled").show()
      $(".btn_disabled").hide()
    }
    else {
       $(".btn_disabled").show()
       $(".btn_enabled").hide()
    }
     });
});
.btn {
                display: none;
            }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<select class="selected" >
                <option>Select Option </option>
                <option>Bank 1</option>
                <option style="color:#cccccc">Bank 2</option>
                <option>Bank 3</option>
            </select>
    
            <button class="btn_enabled btn">Pay Enabled</button>
            <button class="btn_disabled btn">Pay Disabled</button>

Comments

1

$(function () {
  $(".selected").on("change", function (e) {  
    $(".selected option:selected").attr("style") === "color:#cccccc" ? $(".btn_enabled").show() : $(".btn_enabled").hide() 
 });
});
.btn_disabled {
    display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="selected" >
    <option value="1" >Bank 1</option>
    <option value="2" style="color:#cccccc">Bank 2</option>
    <option value="3" >Bank 3</option>
</select>

<button class="btn_enabled btn">Pay Enabled</button>
<button class="btn_disabled btn" disabled>Pay Disabled</button>

1 Comment

What i want if there is color then button added disable attribute but if there is color then no change default enable
1

if there is style="color:#cccccc" in [one of the] option

You can do this with:

$(".selected>option[style='color:#cccccc']").length > 0

eg

$(function() {
  $(".selected>option[style='color:#cccccc']").length > 0 
    ? $(".btn_enabled").show() 
    : $(".btn_enabled").hide()
});

If you instead meant:

if there is style="color:#cccccc" in [the selected] option

then the above can be converted with the use of :selected

$(this).find(">option:selected[style='color:#cccccc']").length > 0

$(function() {
  $(".selected").on("change", function() {
    $(this).find(">option:selected[style='color:#cccccc']").length > 0 ?
      $(".btn_enabled").show() :
      $(".btn_enabled").hide()
  });
  $(".selected").change();
});
.btn_disabled {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="selected">
  <option>Bank 1</option>
  <option style="color:#cccccc">Bank 2</option>
  <option>Bank 3</option>
</select>

<button class="btn_enabled btn">Pay Enabled</button>
<button class="btn_disabled btn" disabled>Pay Disabled</button>

Comments

1

You need use $(this).find(":selected").attr('style') to get selected option style as

 $(".selected").on("change", function () { 
   $(this).find(":selected").attr('style') === 'color:#cccccc' ? $(".btn_enabled").show() : $(".btn_enabled").hide() });
 });

   

$(function () {
$(".selected").on("change", function () { $(this).find(":selected").attr('style') === 'color:#cccccc' ? $(".btn_enabled").show() : $(".btn_enabled").hide() });
    });
    
    //"style" === $(this).attr() ? $(".btn_enabled").show() : $(".btn_enabled").hide() 
.btn_disabled {
            display: none;
        }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="selected" >
            <option>Bank 1</option>
            <option style="color:#cccccc">Bank 2</option>
            <option>Bank 3</option>
        </select>

        <button class="btn_enabled btn">Pay Enabled</button>
        <button class="btn_disabled btn" disabled>Pay Disabled</button>

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.