user1122925
user1122925

Reputation: 237

jQuery - subtract value of data upon a selection

I'm wondering if it is possible to add to a value upon the selection of a drop down list.

For example: If one of the following are selected..

<select class="item_print-colour">
    <option value="0" selected="selected" disabled="disabled">Print Colour..</option>
    <option value="B&W" value="Print Colour:"> Black And White </option>
    <option value="Full-Colour"> Full Colour </option>
</select>

I would like the following data-base_price to be increased by 0.20:

<span class="item_price cd_price" data-base_price="0.53"></span>


What I have already tried but no matter what I try I can't get it to change upon a select:

$(".item_print-colour").on("select", function() {
    var base_price = $(".cd_price").data("base_price");
    var addition = 0;

    if (this.value = B&W) addition = 0.20;
    $(".cd_price").text((base_price + addition).toFixed(2));
    }).trigger("select");

EDIT

I also have other factors determining the data-base_price, which is why I am trying to add to whatever is currently in data-base_price.

Upvotes: 0

Views: 191

Answers (3)

Robert
Robert

Reputation: 8767

Try the following:

$(".dvd_case_quantity").on("change", function() {
    var base_price = $(".cd_price").data("base_price");
    var addition = (this.value == 'B&W' ? 0.20 : 0);
    var newPrice = (base_price + addition).toFixed(2);
    $(".cd_price").text(newPrice);
});

Note: The above solution separates the newPrice into a separate variable. You can keep it as-is, but I try to employ a more debug friendly style which will make testing much easier.

Upvotes: 0

bokonic
bokonic

Reputation: 1771

  • on('change'... not select
  • if(this.value == 'B&W')
  • you can refactor to: base_price += 0.2;
  • you don't need to trigger('select');

Upvotes: 1

Adriano Carneiro
Adriano Carneiro

Reputation: 58615

Perhaps you just missed the quotes?

if (this.value == "B&W")
...

Upvotes: 0

Related Questions