0

My current jQuery selects the value attribute. How do I change my jQuery to select the data attribute "data-price"?

$('#trapfabric, #trapsize').on('change', function() {
  var $selected = $('#trapfabric, #trapsize').children(":selected");
  sum = parseInt($('#trapsize').val()) + parseInt($('#trapfabric').val());
  $('#priceToSwap3').html('$' + sum
    );
});

I know I have to fit something like this in the above but I can't get it to work:

$('#priceToSwap3').text($selected.data("price"))

EDITED

My HTML:

<span id="priceToSwap3"">$238</span>

<select id="trapsize" onChange="swapImage()">
 <option data-price="238">foo</option>
 <option data-price="288">foo</option>
</select>

<select id="trapfabric" onChange="swapImage()">
 <option data-price="0">foo</option>
 <option data-price="20">foo</option>
</select>
5
  • I don't see data-price anywhere in your script. Could you clarify what you mean? Commented Mar 1, 2013 at 11:12
  • It does not makes any sense, why not to use the input value? Commented Mar 1, 2013 at 11:18
  • @Felix Kling I have posted my HTML. Commented Mar 1, 2013 at 11:29
  • @roXon I can't use value because I have more than 1 value to each option. Commented Mar 1, 2013 at 11:30
  • @FelixKling I have edited the jQuery to include $selected. Please take a look. I still can't get it to work. Thanks Commented Mar 1, 2013 at 11:41

3 Answers 3

1

I believe this is what you want:

var $elements = $('#trapfabric, #trapsize');
$elements.on('change', function() {
     var $selected = $elements.children(":selected");
     var sum = 0;

     $selected.each(function() {
         sum += $(this).data('price');
     });

     $('#priceToSwap3').html('$' + sum);
});

You have to iterate over the selected elements to get the price datum of each of them.

DEMO

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

Comments

0

You are binding event to two elements #trapsize, #trapfabric if you want to get the source element you need to use $(this);

jsfiddle

$('#trapfabric, #trapsize').on('change', function() {    
    $('#priceToSwap3').text( '$' + $(':selected', this).data("price") );
});

3 Comments

Hi Adil, I am new to jQuery. Where do I add this line of code so that I could replace the value attribute?
Adil, I thought it would have been weird to make my own answer, since it's really not that different, so I edited your answer after the op gave the related html. @ElijahYang There's a jsfiddle example in the above answer now.
Thanks @Joonas, so nice of you.
0

We had a similar scenario wherein we assign security value(1,2,3 as security level) for each input. We have to change the security values "0" when the user logs into the system.

Eg:

<input type="text" security="3" id="super-admin" />
<input type="text" security="2" id="main-admin" />
<input type="text" security="1" id="normal-user" />

<script>
function userValidation(userType){

if(userType="super-admin"){
   $("[security=1]").attr("security", 0);
   $("[security=2]").attr("security", 0);
   $("[security=3]").attr("security", 0);
 }
if(userType="main-admin"){
  $("[security=1]").attr("security", 0);
  $("[security=2]").attr("security", 0);
 }
 if(userType="normal-user"){
   $("[security=1]").attr("security", 0);
 }
}
<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.