0

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.
});

2 Answers 2

2

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

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

Comments

1

Have you tried:

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

?

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.