Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>EDIT: My original answer was partially correct, in that it changed the value, but it did not fix a more serious bug which I will now explain.</p> <p>The reason my original fix causes ever cell to have the same value is because the jQuery $ method returns every element which matches the given selector. However, calling val() will only get you the value of the first matched element, but setting the value by calling val(value) will set the value of <em>every</em> matched element.</p> <p>The solution is to do the replace above the each(), which will then only sanitize a single element (the <code>this</code>). If you also want to sanitize the totals cells, then you would do <em>that</em> in the each() and use <code>".ttl" + this</code> as the selector instead of <code>this</code>.</p> <pre><code>$('.calc').keyup(function(){ var classArray = $(this).attr('class').split(' '); //Sanitize out here, so we only affect one element. var singleCellVal = $(this).val() singleCellVal.replace(/[A-Za-z$-,]/g, ""); $(this).val(singleCellVal); $.each(classArray, function(){ var totalsum = $('.'+this).sum(); $('.ttl'+this).val(Number(totalsum).toFixed(2)); }); //Finding the grandtotal var grandTotal = $('.row26').parent(). children('td:last').children( 'input'); var sum = $('.row25').parent().children( 'td').children('.calc').sum(); grandTotal.val(Number(sum).toFixed(2)); }); </code></pre> <p>What you want is</p> <pre><code>var value = $('.'+this).val(); value.replace(/[A-Za-z$-,]/g, ""); $('.' + this).val(value); </code></pre> <p>The $ in the character class is fine, most metacharacters in regex are not special within character classes. However, the "<code>$-,</code>" will match any characters that are "between" <code>$</code> and <code>,</code>. I assume this is what you want but if you only want to match <code>$</code> and <code>,</code> and <code>-</code> then you should change that part to "<code>$,-</code>" (i.e. <code>/[A-Za-z$,-]/g</code>). A <code>-</code> at the end of a character class will match the <code>-</code>, anywhere else is seen as a range unless you escape it (<code>\-</code>).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload