Solved

# help extracting and calculate tables cells

Posted on 2013-06-19
176 Views
hi
i have a very complex form, with a section to add option and option values within it. the options section is generated by clicking a button. all this works.

so there can be an option like "color" and the values can be: red,green,blue.
and say "size" and the values can be : small,medium,large,xtra-large.

now i need to take the sum for EACH OPTION QUANTITY: lets say- the size option has 100 for small, 20 for large and so on.
i need the sum quantity for each option. so the sum of size will be 120.

i left the html as simple as i could to help you to help me, this is the html code:
``````        <div id="currentopts">
<div class="optis" id="option158">
<table class="optiontable">
<tr>
<th>Option name</th>
<th>Position</th>
<th>Action</th>
</tr>
<tbody>
<tr>
<td><input type="text" value="color" name="158" class="optclass"></td>
<td><input type="text" size="5" value="0" name="158" class="optpos"></td>
<td>
<a class="remove_optionup" id="remove_158" href="#">Remove</a>
</td>
</tr>
</tbody>
</table>
<div class="optvalis" id="optionvalue_88">
<table class="optiontable">
<tr>

<th>Quantity</th>

</tr>
<tbody>
<tr>
<td><input type="text" size="5" value="60" name="quantity" class="theqty"></td>
<td>
<a class="remove_option_valueup" id="88" href="#">Remove</a>
</td>
</tr>
</tbody>
</table>
</div>
<div class="optvalis" id="optionvalue_87">
<table class="optiontable">
<tr>

<th>Quantity</th>

</tr>
<tbody>
<tr>
<td><input type="text" size="5" value="20" name="quantity" class="theqty"></td>
<td><a class="remove_option_valueup button orange" id="87" href="#">Remove</a></td>
</tr>
</tbody>
</table>
</div>
<div class="optvalis" id="optionvalue_89">
<table class="optiontable">
<tr>

<th>Quantity</th>

</tr>
<tbody>
<tr>
<td><input type="text" size="5" value="30" name="quantity" class="theqty"></td>
<td><a class="remove_option_valueup" id="89" href="#">Remove</a></td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="optis" id="option156">
<tr>
<th>Option name</th>
<th>Position</th>
<th>Action</th>
</tr>
<tbody>
<tr>
<td><input type="text" value="size" name="156" class="optclass"></td>
<td><input type="text" size="5" value="0" name="position" class="optpos"></td>
</tr>
</tbody>
</table>
<div class="optvalis" id="optionvalue_82">
<table class="optiontable">
<tr>

<th>Quantity</th>

</tr>
<tbody>
<tr>
<td><input type="text" size="5" value="70" name="quantity" class="theqty"></td>
<td><a class="remove_option_valueup" id="82" href="#">Remove</a></td>
</tr>
</tbody>
</table>
</div>
<div class="optvalis" id="optionvalue_83">
<table class="optiontable">
<tr>

<th>Quantity</th>

</tr>
<tbody>
<tr>
<td><input type="text" size="5" value="50" name="quantity" class="theqty"></td>
<td><a class="remove_option_valueup button orange" id="83" href="#">Remove</a></td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="optis" id="option157">
<tr>
<th>Option name</th>
<th>Position</th>
<th>Action</th>
</tr>
<tbody>
<tr>
<td><input type="text" value="sleeves" name="157" class="optclass"></td>
<td><input type="text" size="5" value="0" name="position" class="optpos"></td>
</tr>
</tbody>
</table>
<div class="optvalis" id="optionvalue_86">
<table class="optiontable">
<tr>

<th>Quantity</th>

</tr>
<tbody>
<tr>
<td><input type="text" size="5" value="20" name="quantity" class="theqty"></td>
<td><a class="remove_option_valueup" id="86" href="#">Remove</a></td>
</tr>
</tbody>
</table>
</div>
<div class="optvalis" id="optionvalue_85">
<table class="optiontable">
<tr>

<th>Quantity</th>

</tr>
<tbody>
<tr>
<td><input type="text" size="5" value="70" name="quantity" class="theqty"></td>
<td><a class="remove_option_valueup button orange" id="85" href="#">Remove</a></td>
</tr>
</tbody>
</table>
</div>
<div class="optvalis" id="optionvalue_84">
<table class="optiontable">
<tr>

<th>Quantity</th>

</tr>
<tbody>
<tr>
<td><input type="text" size="5" value="50" name="quantity" class="theqty"></td>
<td><a class="remove_option_valueup" id="84" href="#">Remove</a></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
``````

best regards
0
Question by:derrida
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 42

Accepted Solution

sedgwick earned 500 total points
ID: 39261807
``````	var options = {};
\$('.optis').each(function() {
var sum=0;
var optionName = \$(this).find('input[class="optclass"]').attr("value");
\$(this).find('input[name="quantity"]').each(function(){
sum+=parseInt(\$(this).attr("value"));
});
options[optionName] = sum;
});
``````

the options variable maps option name (color,size etc) and the sum of the quantities, so u ended up with:
options["color"] = 110
options["size"] = 120
options["sleeves"] = 140
0

LVL 1

Author Closing Comment

ID: 39261835
you just saved my life. thanks so much
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

Link Anchor Not working 9 67
Why is my \$_POST not going to results page 10 68
bootstrap form validations! 1 40
Cannot find JQuery answer to looping through nested objects 8 32
Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
###### Suggested Courses
Course of the Month5 days, left to enroll