2

I have dynamically generated text boxes and want to do calculations with their values using jquery.

The text box names are as follows :

A[1] , B[1]  - on change event , Alert SUM of values of A[1] and B[1]

A[2] , B[2]  - on change event , Alert SUM of values of A[2] and B[2] 

A[3] , B[3]   - on change event , Alert SUM of values of A[3] and B[3]

.
.
.
.
A[10] , B[10]   - on change event , Alert SUM of values of A[10] and B[10]

How to make a function in jquery for this task. Please help me in accomplishing this task.

I have this code but its for all the text boxes A and B , but I want to specify SETS OF VALUES LIKE SUM of A[1] and B[1] , SUM of A[2] and B[2] .......

$("input[type='text'][name^='A'] ,input[type='text'][name^='B']").
keyup(function(){ 

alert(sumThemUp());

});


function sumThemUp(){
var sum = 0;

$("input[type='text'][name^='A'] ,input[type='text'][name^='B']").
each(function(){

sum+= parseInt($(this).val());

});

return sum;

    };

My HTML is:

<input type="text" name="A[1]">
<input type="text" name="B[1]">
<input type="text" name="A[2]">
<input type="text" name="B[2]">
.
.
.
.
.<input type="text" name="A[10]">
<input type="text" name="B[10]">
5
  • 1
    What have you tried? Commented May 18, 2012 at 17:26
  • I don't know what to do ? How do fetch the values of A[1] and B[1] using a function Commented May 18, 2012 at 17:36
  • You might want to begin by posting your code. Commented May 18, 2012 at 17:36
  • Give your DOM elements a class, query that class, do work with the result set. If you want it to update on blur, then use the jquery .on() method to fire off your script. I assume your results go at the end of each column and the end of each row? You need to elaborate your requirements. Commented May 18, 2012 at 17:38
  • i have posted some code please check it out Commented May 18, 2012 at 17:44

2 Answers 2

1

I think you're trying something like this:

$('input[type=text]').on('keyup', function() {
    var cur = this.name;
    if(cur.indexOf('A') == 0){
       var target = 'B' + cur.replace('A','');
    } else target = 'A' + cur.replace('B','');
    var targetVal = $('input[name="'+ target +'"]').val();
    var sum = parseInt(this.value) + parseInt(targetVal ? targetVal: 0);
    console.log(sum);
});

Working Demo

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

3 Comments

sir, i want to select the value of this textbox with name : A[1] using this selector : var id = 1; ------------------------------------------------- var a = $("input[type='text'][name='A["+id+"]").val(); --------------------------------------------------is my syntax correct?
use this: $('input[type=text][name="A['+ id +']"]');
yes sir i will work on it too...will tell as soon as i complete...thanks for your great help
1

This is probably the simplest way to do it that I can think of. It will require a slight modification to your dynamic DOM stuff, but I assume you always generate an A and a B, so this shouldn't be a problem.

HTML:

<span class="calc_group">
    <input type="text" name="A[1]">
    <input type="text" name="B[1]">
</span>
<span class="calc_group">
    <input type="text" name="A[2]">
    <input type="text" name="B[2]">
</span>

jQuery:

NOTE: this will NEED to be called within your dynamic DOM function on every addition!

function doAttachment(){
     $('.calc_group input').unbind('keyup');

     $('.calc_group input').keyup(function(){
           var sum = 0;
           $(this).parent().children().each(function(){
                sum += parseInt($(this).val());
           });

           alert(sum);
      });
}

1 Comment

sir, i want to select the value of this textbox with name : A[1] using this selector : var id = 1; ------------------------------------------------- var a = $("input[type='text'][name='A["+id+"]").val(); --------------------------------------------------is my syntax correct?

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.