chespinoza
chespinoza

Reputation: 2658

How to compare two input boxes in a variable table of input boxes?

I'm trying to alert when a input box is greater than a second box, in example:

<td align="center"> <input type='text' name='cantidad{{=i+1}}' id='cantidad' value='{{=(x.cantidad)}}' size='3' readonly="readonly"  /></td>

<td align="center"> <input type='text' name='desp{{=i+1}}' id='desp' value='' size='3' required='required' placeholder='Numero' onBlur="return validar(event)"/></td>

I have x set of input boxes, among these, the last two input boxes that I need check that the second can't be greater than the first. Then I can't figure out how catch these values in table that can be x set of them

The {{}} code is Python Embedded.

Edited: There are 12 input boxes for each row
Edited: I need that input with id='desp' can't be greater that the input with the id='cantidad' on every row

Upvotes: 1

Views: 220

Answers (2)

Rahul Tripathi
Rahul Tripathi

Reputation: 172398

You can try by giving the ID's something like this:-

if(document.getElementById("first").value == document.getElementById("second").value){
//they are the same, do stuff for the same
}else if(document.getElementById("first").value >= document.getElementById("second").value
//first is more than second
}

or you can do something like this in the JSFiddle

Upvotes: 1

Steven Hunt
Steven Hunt

Reputation: 2321

If you add a css class to the last textbox, say we call it "textbox-last", then you can use jQuery to bind a function to the blur event as follows:

$('#yourtable').on('blur', '.textbox-last', function() {

    var td = $(this).parent();
    var prevTextboxValue = td.prev().find('input').val();

    ...

});

That gets you the value from the previous textbox for any textbox that has the class on it. Then add whatever logic you want to implement after that.

Upvotes: 0

Related Questions