7

I'm trying to get the class of the selected option and use that in a compare statement to find which class it is.

I get an undefined value. The option tag has a class set to it.

Why am I not getting a value back?

Html:

<select name="datagrid_filter" onchange="startFilter();">
    <option>All</option>
    <option disabled="true">Assignment:</option>
    <option disabled="true">Client:</option>
    <option disabled="true">Type:</option>
    <option class="type" value="Activity"> &nbsp Activity</option>
    <option class="type" value="Alert"> &nbsp Alert</option>
    <option class="type" value="Lead"> &nbsp Lead</option>
    <option class="type" value="Notification"> &nbsp Notification</option>
</select>

Javascript:

var cs1 = $("option:selected", this).attr("class");
if(cs1 == 'Type'){
    //do something
}
2
  • 1
    change 'Type' to 'type' in js Commented Sep 21, 2013 at 14:14
  • i made the change, but the problem is that cs1 is undefined Commented Sep 21, 2013 at 14:20

4 Answers 4

18

You should change:

onchange="startFilter();" to onchange="startFilter(this);"

and in startFilter function:

function startFilter(ele){
    var className = $("option:selected", ele).attr("class");
    if(className == 'type'){
        //do something
    }
}

Take note that, the this inside startFilter function refers to the window object, not the element you bind it to.

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

1 Comment

Worked great, like in the above statement, I was referring to wrong object with my this. Needed to make sure right object was referenced. thanks
1

Why don't you use hasClass jQuery API? It's very good in this case:

if($("option:selected", this).hasClass('type')){
    //do something
}

And make sure this referes to a proper object (as @undefined mentioned)
Or use:

if($("option:selected", "select[name=datagrid_filter]").hasClass('type')){
        //do something
    }

Or:

if($("select[name=datagrid_filter] option:selected").hasClass('type')){
            //do something
 }

Comments

0

Try not to have inline JavaScript

Fiddle Demo

JavaScript

   $(function()
    {
        var handleChange    =   function()
                                {
                                    var $this     = $(this);
                                    var $selected = $this.children(":selected");
                                    var isType    = $selected.hasClass('type');
                                    if(isType)
                                    {
                                        alert('Do Something');                        
                                    }
                                };
        var $datagridFilter =  $('[name=datagrid_filter]')

        $datagridFilter.change(handleChange);

    });

Comments

0
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select onchange="getval(this);" class="sele">
   <option>All</option>
    <option disabled="true">Assignment:</option>
    <option disabled="true">Client:</option>
    <option disabled="true">Type:</option>
    <option class="type" value="Activity"> &nbsp Activity</option>
    <option class="type" value="Alert"> &nbsp Alert</option>
    <option class="type" value="Lead"> &nbsp Lead</option>
    <option class="type" value="Notification"> &nbsp Notification</option>
</select>
<script>
 function getval(sel)
{

    if($('.sele :selected').attr('class'))
    {
      console.log($('.sele :selected').attr("class","typ123"));
          // Your code write here
    }

}
</script>

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.