0
<div class="mt-repeater-item">
    <div class="row mt-repeater-row">
        <input type="text" name="product[0][price]" value="2" oninput="update_total(this)">
    </div>
</div>
<div class="mt-repeater-item">
    <div class="row mt-repeater-row">    
        <input type="text" name="product[1][price]" value="3" oninput="update_total(this)">
    </div>
</div>

And my script

function update_total(input) {
    var sum = 0;
    $('.mt-repeater-item').each(function(index) {
        var product_price = $("input[name=product["+index+"][price]]").val() ? $("input[name=product["+index+"][price]]").val() : 0;
        sum = sum + product_price;
    });
    alert(sum);
}

Error Uncaught Error: Syntax error, unrecognized expression => How to fix it?

2 Answers 2

1

You have missing quotation for name --

function update_total(input) {
    var sum = 0;
    $('.mt-repeater-item').each(function(index) {
        var product_price = $("input[name='product["+index+"][price]']").val() ? $("input[name='product["+index+"][price]']").val() : 0;
        sum = sum + product_price;
    });
    alert(sum);
}

Link - https://jsfiddle.net/80op2y19/

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

Comments

1
function update_total(input) {
    var sum = 0;
    $('.mt-repeater-item').each(function(index) {
            var product_price = $("input[name='product["+index+"][price]']").val() ? $("input[name='product["+index+"][price]']").val() : 0;
        sum = sum + parseInt(product_price);
    });
    alert(sum);
}

enclose value of name inside single quotes

1 Comment

Also use parseInt. sum = sum + parseInt(product_price);

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.