Vishnu
Vishnu

Reputation: 2452

Get custom attribute value in multiple select

I have many select elements like below , When any option is selected I need to get the value of data-cal

  <select data-cal="89" name="caloriecalculator" style="width: 100px;">
                      <option value="100">Per 100g</option>
                      <option value="225">1 cup, mashed </option>
                      <option value="150">1 cup, sliced </option>
    </select>

    <select data-cal="109" name="caloriecalculator" style="width: 100px;">
                      <option value="100">Per 100g</option>
                      <option value="225">1 cup</option>
                      <option value="150">1 cup big</option>
    </select>

My jquery code

$('select[name="caloriecalculator"]').change(function() {
//I need to get the data atribute (data-cal) of select here.
});

Upvotes: 0

Views: 541

Answers (2)

Oleksandr T.
Oleksandr T.

Reputation: 77482

You can use $.data method, like this

$('select[name="caloriecalculator"]').change(function() {
  console.log($(this).data('cal') );
});

Demo: http://jsbin.com/letoje/1/edit?html,js,output

Upvotes: 2

JLRishe
JLRishe

Reputation: 101652

Have you tried:

$('select[name="caloriecalculator"]').change(function() {
    var cal = $(this).data('cal');
});

?

Upvotes: 1

Related Questions