Cjueden
Cjueden

Reputation: 1200

jQuery cannot find custom attribute

jQuery cannot find my custom attribute on a drop down menu I have a similar one just one line above it that works great, but this keeps giving me an undefined.

jQuery

$('#admin_student_school_select').change('click',function(){
var school_student = $(this+':selected').attr('school_student');
$('#admin_student_content_details')
  .html(ajax_load)
  .load(loadUrl,"form_being_submitted=admin_page_list_student&school="+school_student);
});

HTML

<select id="admin_student_school_select">
    <option>Select a School</option>
    <option school_student="1">Riverside Community College</option>
    <option school_student="2">Victor Valley College</option>
    <option school_student="3">Cal State San Bernardino</option>
    <option school_student="4">Craffton College</option>
    <option school_student="5">San Bernardino Community</option>
</select>

The script behind the ajax call works. I've echoed out results.

Upvotes: 1

Views: 954

Answers (3)

gdoron
gdoron

Reputation: 150253

This line is invalid:

$('#admin_student_school_select').change('click',function(){

What is it, a click or a change event?!


Update:

$(this+':selected') isn't a valid selector...use this:

var school_student = $(':selected', this).attr('school_student');

Upvotes: 3

Ram
Ram

Reputation: 144689

try this one:

$('#admin_student_school_select').change(function(){
     var school_student = $(this).val();
});

<select id="admin_student_school_select">
    <option value="0">Select a School</option>
    <option value="1">Riverside Community College</option>
    <option value="2">Victor Valley College</option>
    <option value="3">Cal State San Bernardino</option>
    <option value="4">Craffton College</option>
    <option value="5">San Bernardino Community</option>
</select>

Upvotes: 0

Andreas Wong
Andreas Wong

Reputation: 60516

I don't think $(this+':selected') would return anything useful.

Try:

$('#admin_student_school_select').change(function(){
   var school_student = $(':selected', this).attr('school_student');
   // rest of your code
});

Upvotes: 2

Related Questions