简体   繁体   中英

How to detect a change in any <td> of table, using javascript?

I have a Table whose <td> values varies depending upon the inputs given in form, I am using Tangle to make a reactive document. Is it posible to detect if the value of <td> changes to any negative number? If so, then it must change its color to red!
Can Javascripting or html tags itself solve this problem?
Please help!
My change will be on profitLossIn1,profitLossIn2,profitLossIn3.
Here is my html:

<table>
    <tr>
        <th>Name</th>
        <th>Cost</th>
        <th>Revenue</th>
        <th>Result Profit/Loss</th>
    </tr>
    <tr>
        <td><input id='NameInn1' type='text' NAME="NameInn1"></td>
        <td><span class="TKNumberField" data-var="CostIn1"></span></td>
        <td><b data-var="revenueIn1"> Cost</b></td>
        <td><b data-var="profitLossIn1"> dollars</b></td>
    </tr>
    <tr>
        <td><input id='NameInn2' type='text' NAME="NameInn2"></td>
        <td><span class="TKNumberField" data-var="CostIn2"></span></td>
        <td><b data-var="revenueIn2"> Cost</b></td>
        <td><b data-var="profitLossIn2"> dollars</b></td>
    </tr>
    <tr>
        <td><input id='NameInn3' type='text' NAME="NameInn3"></td>
        <td><span class="TKNumberField" data-var="CostIn3"></span></td>
        <td><b data-var="revenueIn3"> Cost</b></td>
        <td><b data-var="profitLossIn3"> dollars</b></td>
    </tr>

    <tr>
        <td></td>
        <td></td>
        <td></td>
        <td><b data-var="totalRevenueIn"> dollars</b></td>
    </tr>
</table>

I am trying this:

var inputs = document.getElementById("profitLossOut1");
console.log(inputs.value);
inputs.onchange = function () {
    console.log("Checking IF condition");
    if ((parseInt(this.value)).match("-") == true) this.parentNode.style.background = "red";
};

maybe this can be helpful: http://jsfiddle.net/tz7WB/

JQUERY CODE

$(":input").on("change", function () {
    if (parseInt($(this).val()) < 0) $(this).closest("td").css("background", "red");
})

try to type any negative number in the inputs


with pure js: http://jsfiddle.net/tz7WB/1/

JS CODE

var inputs = document.getElementsByTagName("input");
for (var x = 0; x < inputs.length; x++) {
    inputs[x].onchange = function () {
        if (parseInt(this.value) < 0) this.parentNode.style.background = "red";
    };
}

Assuming I understand your question correctly, you don't want to check for negative values of the inputs , you want to check for negative values of the tangle-generated text values.

This code should be more of what you're looking for, and it works as long as the text content of your data-var elements starts with the number. If not, the number parsing logic will need to be improved:

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $("input").change(function(e) {
        var $target = $(e.target);
        var checkNeg = function(c) {
            for(var i=0,$ci; i<c.length; i++) {
                $ci = $(c[i]);
                if(parseInt($ci.text()) < 0) $ci.css("color", "red");
                else $ci.css("color", "black");
            }
        };
        checkNeg($target.parents("table").find("[data-var]"));
    });
});
</script>

Consider using knockout.js for your scenario, you can use it to easily format your UI elements according to the underlying data. Take a look at the examples: http://knockoutjs.com/examples/

Add jquery change events for each of input boxes . Below is sample code

$("#NameInn1").change(function(){
   var input = $("#NameInn1").val();
   if(input<10){
     $("#NameInn1").css("background-color","red");
   }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM