0

I would like the script to multiply values from input boxes by the values of hidden inputs, and print the result upon clicking the button.

Script pretty much does it but only for 1 input.

Any easy way to do it?

Much appreciated.

Fiddle: http://jsfiddle.net/eN7S6/3/

 <table>
        <tr>
            <td>
    <input name="inputone" id="inputone" class="calc" value="1"><span id="Total"></span>
                </td>
        </tr>
            <tr>
            <td>
    <input name="inputone" id="inputone" class="calc" value="1"><span id="Total"></span>
                </td>
                </tr>
    </table>
        <input name="inputtwo" id="inputtwo" class="calc" value="2" type="hidden" readonly>
        <input name="inputtwo" id="inputtwo" class="calc" value="3" type="hidden" readonly>
        <input type="button" id="update" value="Calculate" />

And jQuery

$(document).ready(function() {      
    $('#update').click(function() { 
        var inputone = $('#inputone').val();
        var inputtwo = $('#inputtwo').val();
        var totalTotal = ((inputone * 1) * (inputtwo * 1));     
        $('#Total').text(totalTotal);
    });
}); 
1
  • 3
    ids must be unique on a page. Commented Nov 12, 2013 at 0:59

3 Answers 3

1

Your element IDs are not unique. You have duplicates, such as "inputone" and "inputtwo".

I've changed their names, and added a new total span.

Here's the updated code:

<table>
    <tr>
        <td>
<input name="inputone" id="inputone" class="calc" value="1"><span id="TotalOne"></span>
            </td>
    </tr>
        <tr>
        <td>
<input name="inputtwo" id="inputtwo" class="calc" value="1"><span id="TotalTwo"></span>
            </td>
            </tr>
</table>
    <input name="multiplierone" id="multiplierone" class="calc" value="2" type="hidden" readonly>
    <input name="multipliertwo" id="multipliertwo" class="calc" value="3" type="hidden" readonly>
    <input type="button" id="update" value="Calculate" />

and jQuery:

$(document).ready(function() {      
    $('#update').click(function() { 
        var inputone = $('#inputone').val();
        var multiplierone = $('#multiplierone').val();
        var inputtwo = $('#inputtwo').val();
        var multipliertwo = $('#multipliertwo').val();
        var totalTotalOne = (inputone * multiplierone);
        var totalTotalTwo = (inputtwo * multipliertwo);
        $('#TotalOne').text(totalTotalOne);
        $('#TotalTwo').text(totalTotalTwo);
    });
}); 

and updated jsFiddle: http://jsfiddle.net/eN7S6/4/

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

Comments

0

This little algorithm will get all the input.calc elements and multiply their values. Think this should work for you based on your given html.

var total = 1;
$("input.calc").val(function(idx, val) {
    total*= val;
})
$('#Total').text(total);

JSFiddle demo

Comments

0

There can never be two IDsDon the same page that are the same, that's why the class was invented.

Modify your code to something like this:

 <table>
   <tr>
     <td>
         <input name="inputone"class="inputone calc" value="1"><span id="Total"></span>
     </td>
   </tr>
   <tr>
     <td>
       <input name="inputone" class="inputone calc" value="1"><span id="Total"></span>
     </td>
   </tr>
 </table>
 <input name="inputtwo" class="calc inputtwo" value="2" type="hidden" readonly>
 <input name="inputtwo" class="calc inputtwo" value="3" type="hidden" readonly>
 <input type="button" id="update" value="Calculate" />

Then:

$(document).ready(function() {      
    $('#update').click(function() { 
        var inputone = $('.inputone').val();
        var inputtwo = $('.inputtwo').val();
        var totalTotal = ((inputone * 1) * (inputtwo * 1));     
        $('#Total').text(totalTotal);
    });
}); 

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.