0

I have copy from Comparing form input and span value for keyup function ,

    while($row = $results->fetch_array()) {
    ......
        <td><input type="text" name="qtydo[]" value="<?php echo $row['qty'];?>" autocomplete="off" class="form-control" id="one_and_done_stool_QTY_remain" required="" disabled ></td>
        <td><input type="text" name="qty[]" value="" autocomplete="off" class="form-control" id="one_and_done_stool_QTY_check" maxlength="7" required=""></td>
}

js

    <script>
document.getElementById('one_and_done_stool_QTY_check').addEventListener('input', Qty_check);


function Qty_check(e) {
    var QTY1check = +document.getElementById('one_and_done_stool_QTY_check').value;
    var QTY1remain = +document.getElementById('one_and_done_stool_QTY_remain').value;

    if (QTY1check > QTY1remain) {
        console.log("NOPE");
        document.getElementById("one_and_done_stool_QTY_check").value = 0;
    }
}
</script>

table not working in second row

first row succeeds but second row fails , need help.

thanks

  • 2
    You're using the same ID on multiple elements, IDs are meant to be unique, use class names instead. – Titus Aug 06 '19 at 00:15
  • sorry , how to make different ID if row generate from result db and same id with function? – Agusto Nice Aug 06 '19 at 00:23
  • Can you change the server code ? If so, add class names to the inputs, they don't need IDs. – Titus Aug 06 '19 at 00:25
  • change id with class ? already triend not working – Agusto Nice Aug 06 '19 at 00:28
  • Yes, beside that you'll also have to change the JS to use `getElementsByClassName` and in the `Qty_check` function use the event (`e` object) to determine which input was clicked and get the other input on the same table row. – Titus Aug 06 '19 at 00:30
  • 1
    @AgustoNice you'll also need to be aware that `document.getElementsByClassName()` returns a collection of elements (not just one), and so you'll need to add your input event listener using a loop (see how to do this here: https://stackoverflow.com/questions/19655189/javascript-click-event-listener-on-classA). You could also use the `this` keyword to check which element was clicked as well – Nick Parsons Aug 06 '19 at 00:31
  • `@Tutus` and `@Nick Parsons`, what is the point of that advice? When possible, just attach the Event to the Element it should be attached to, instead of testing for `EventObject.target`. I mean... it sounds like you're suggesting to loop over all the inputs, which you wouldn't need to add a class for that, by the way... not to mention the Browser would be doing unnecessary work. – StackSlave Aug 06 '19 at 00:54

0 Answers0