Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincej's avatar
Level 15

JQuery: How to select an individual option in a drop down select

I have a table with many rows. Each row, contains a drop drown select with only two 2 options. When the user selects "active" he gets an alert message. If he selects deactivated, he gets a different alert message. The code I have works badly. It only shows one single alert no matter which option is selected. Secondly, the .on(click function()) only fires once per page refresh. I have checked SO a million times, and tried every thing known.

Any advice ? Many thanks !!

From the VIew

<td class="category_select" style="text-align: center">{{ Form::select("category_value[]",['active'=>'Active' ,'deactivated' => 'Deactivated'],$category->status,['class'=>'edit_cat form-control'])}}</td>

JQuery

$("select.edit_cat").on("click",function() {
           if($('select[name="category_value[]"]').find('option:contains("activated")').prop("selected",true))
           {$(".category_alert").html('<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong>INFO:</strong> Message One ').addClass("alert alert-info alert-dismissible")}

             else{ $(".category_alert").html('<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong>WARNING:</strong> Message Two ').addClass("alert alert-danger alert-dismissible")}
        });

0 likes
6 replies
vincej's avatar
Level 15

Thanks for that. no luck. I have tried every solution on SO. None so far work. Some do not even work on SO!

vincej's avatar
Level 15

Thank you very much!! This set me on the right track, and although the code above is not exactly what I needed it really helped me get it right. I had completely overlooked the use of this.

Three cheers !

Cronix's avatar

I don't know what your if() statement is evaluating. You're setting a prop in it, not getting a value to compare if something is true.

vincej's avatar
Level 15

I have changed the code entirely since I since I posted that. I was trying to evaluate whether the option was activate or deactivated. Now I remember the use of this, I came up with, which works fine:

 $(".category_select").on("change",".edit_cat", function() {
        var deactivated=$(this).val()==="active"
        if(deactivated == true){
            $(".category_alert").html('<strong>INFO:</strong> Message One ').removeClass("alert-danger").addClass("alert alert-info alert-dismissible")
            }
            else{$(".category_alert").html('<strong>WARNING:</strong> Message Two ').removeClass("alert-info").addClass("alert alert-danger alert-dismissible");

        }

    })

The only problem I now have is working with Jeffrey Way's old v 2.0 flash package. Going to rip all that stuff out of my code and use Laravel flash alerts instead!

Please or to participate in this conversation.