Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
52
rated 0 times [  56] [ 4]  / answers: 1 / hits: 88095  / 11 Years ago, sat, september 21, 2013, 12:00:00

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
}

More From » jquery

 Answers
68

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.


[#75541] Friday, September 20, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ryanulyssesb

Total Points: 91
Total Questions: 105
Total Answers: 102

Location: England
Member since Tue, Sep 8, 2020
4 Years ago
ryanulyssesb questions
Sat, Mar 20, 21, 00:00, 3 Years ago
Mon, Sep 14, 20, 00:00, 4 Years ago
Mon, Mar 9, 20, 00:00, 4 Years ago
Sun, Jul 7, 19, 00:00, 5 Years ago
;