0

I'm trying to take value from input field, do simple calculation and display the answer to the form element.

<script type="text/javascript">
    $(document).ready(function () {
        $("#number_ticket").change(function () {
            var adult = parseInt($(this).val()) * parseInt($('#price_adults').textContent);
            $('#total_price_adults').text(adult);
        });
    });
</script>

Here's my HTML

<div class="st_adults_children">
    <span class="label">Adults</span>
    <div class="input-number-ticket">
        <input type="number" name="number_ticket" value="1" min="1"
                                                           max="10" placeholder="Number ticket of Adults"
                                                           id="number_ticket">
    </div>
    ×
    $<span class="price_adults" id="price_adults">{{$single->price}}</span>
    =
    <span class="total_price_adults" id="total_price_adults">$93</span>
</div>

I've tried removing parseInt() from $(this).val(), since the value is already integer. I couldn't work out what's wrong here.

3 Answers 3

2

The issue is with $('#price_adults').textContent. jQuery objects don't have a textContent property, DOM Nodes do.

The jQuery method is .text():

var adult = parseInt($(this).val()) * parseInt($('#price_adults').text());

If you want to use textContent then you can access the DOM element:

var adult = parseInt($(this).val()) * parseInt($('#price_adults')[0].textContent);
//                                                               ^^^ get the DOM node
Sign up to request clarification or add additional context in comments.

1 Comment

Using .text() is still giving me 'NaN' result.. Do we need to parse the result too?
0

There are a few ways you can improve what you've got:

First - when you're working in javascript, look up and cache elements and values where you can. For example, if you know the ticket price will not change after the page is loaded, you can look up the number once, and then re-use it. Basically, you shouldn't be going off to the DOM to find elements or values inside an event handler if you don't need to.

Second tip - you can use some simple CSS to format prices, so that your elements can be the straight value, and you don't need to worry about coercing back and forth between '$x.xx' and 'x.xx'.

Third, you probably don't want to parseInt on currencies, since they can contain cents. parseFloat would retain your cents, though you may not need it if you have control over the values.

Try this (fiddle)

HTML:

<div class="st_adults_children">
    <span class="label">Adults</span>
    <span class="input-number-ticket">
        <input type="number" name="number_ticket" value="1" min="1" max="10" placeholder="Number ticket of Adults" id="number_ticket">
    </span>
    ×
    <span class="price price_adults" id="price_adults">10</span>
    =
    <span class="price total_price_adults" id="total_price_adults"></span>
</div>

JS

//Cache the elements you're interested in
var ticketPrice = $('#price_adults').html(),
        totalElement = $('#total_price_adults'),
    quantityInput = $('input[name=number_ticket]');

//Watch change as well as click/keyup (improves the responsiveness of the number input controls)
quantityInput.on('change click keyup', function () {
  totalElement.html(quantityInput.val() * ticketPrice);
});

//Initialise on ready
quantityInput.trigger('change');

CSS:

.price::before {
  content: '$';
}

Comments

0

you can use .html() instead of .text() property.

like , parseInt($('#price_adults').html());

hope this will help you.

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.