0

How do i add values of input of same class. I have tried the code below but doesn't seem to work. Where am i going wrong?

<table id="tableID">
<tr>

<td>   <input name="name" class="compulsory1" type="text" value="1" />  </td>
<td>   <input name="name1" class="compulsory1" type="text" value="2" />  </td>
<td>   <input name="name2" class="compulsory1" type="text" value="3" />  </td>

<td>
<script type="text/javascript">
var tdsCompulsory = document.getElementsByClassName('compulsory1')[0].value;
var cData = [];
sum = 0;
for(var i in tdsCompulsory){
    if(typeof tdsCompulsory[i].textContent != 'undefined')
    cData.push(tdsCompulsory[i].textContent);
}
console.log(cData);

for(var i in cData){
    sum +=parseInt(cData[i]);
}
alert (sum);
</script>

</td>

</tr>
</table>

4 Answers 4

2

try this , use querySelectorAll , You will get all elements in the document with class="compulsory1":

window.onload = function(){
var x = document.querySelectorAll(".compulsory1");
var total = 0;
for(var i=0;i<x.length;i++)
{
total+=Number(x[i].value);
}
console.log(total);
}
  <input name="name" class="compulsory1" type="text" value="1" /> 
  <input name="name1" class="compulsory1" type="text" value="2" /> 
  <input name="name2" class="compulsory1" type="text" value="3" /> 

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

Comments

2

In your solution tdsCompulsory is not a array, it's just a number of first element(input).

var tdsCompulsory = document.getElementsByClassName('compulsory1');
var len = tdsCompulsory.length;
var cData = [];
sum = 0;
for(var i = 0; i < len; i++){
    cData.push(tdsCompulsory[i].value);
    sum += +tdsCompulsory[i].value 
}
alert (sum);
<table id="tableID">
<tr>

<td>   <input name="name" class="compulsory1" type="text" value="1" />  </td>
<td>   <input name="name1" class="compulsory1" type="text" value="2" />  </td>
<td>   <input name="name2" class="compulsory1" type="text" value="3" />  </td>

<td>

.

Comments

1

Here

var tdsCompulsory = document.getElementsByClassName('compulsory1')[0].value;

your tdsCompulsory is a single element, because you get [0] element.

Change your code to

var tdsCompulsory = document.getElementsByClassName('compulsory1');

for(var i = 0; i < tdsCompulsory.length; i++){
    if(typeof tdsCompulsory[i].value != undefined){
       cData.push(tdsCompulsory[i].textContent);
    }
}

    console.log(cData);

for(var i in cData){
    sum +=parseInt(cData[i]);
}
alert (sum);

or use foreach method

[].foreach.apply(tdsCompulsory, (el) => {
   if(typeof tdsCompulsory[i].value != undefined){
        cData.push(tdsCompulsory[i].value);
     }
});

2 Comments

Does that capture the values?
When i use ur code like this. it doesnt alert total. could you help with a jsfiddle <script type="text/javascript"> var tdsCompulsory = document.getElementsByClassName('compulsory1').value; for(var i = 0; i < tdsCompulsory.length; i++){ if(typeof tdsCompulsory[i].textContent != 'undefined') cData.push(tdsCompulsory[i].textContent); } console.log(cData); sum = 0; for(var j in cData){ sum +=parseInt(cData[j]); } alert (sum); </script>
1

You can write like below using jQuery:

   <table id="tableID">
                <tr>
                <td>   <input name="name" class="compulsory1" type="text" value="1" />  </td>
                <td>   <input name="name1" class="compulsory1" type="text" value="2" />  </td>
                <td>   <input name="name2" class="compulsory1" type="text" value="3" />  </td>
                <td>
        </table>
        <input type="Button" value="Show Sum" onclick="sumAll()">


     <script> 
        function sumAll(){
            var values = $('.compulsory1');
            var sum = 0;
            for (var i =0; i<values.length; i++) {
            console.log(values[i]);
            sum += parseInt($(values[i]).val());
        }
        alert(sum);
     }
    </script>

Thanks, Ravi

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.