vladimir vladimir - 16 days ago 7
Javascript Question

How to check if one value is greater than other

I have some problems with this checking in jQuery.

Problem:

I have dynamically generated table. In this table I have many rows. In each row I have checkbox and input. If input is not empty then checkbox checked automatically on input blur.

e.g. I have two filled inputs with some values and checkboxes are checked also. I want to check if the input value is greater then some value.

Code:

$("#btn_send_order")
.on("click", function(){

var keys = new Array(),
array = new Array();

$(':checkbox[class="send_vacc checkbox"]:checked').each (function()
{
if ( parseInt($("input[id='"+this.id+"'].sended_cnt").val()) > parseInt($("input[id='"+this.id+"'].sended_cnt").attr("class").split(" ")[2]) )
{
return false;
}
else
{
array.push( $("input[id='"+this.id+"'].sended_cnt").val() );
keys.push( this.id );
}
});
});


So, I have a value which is entered into input and second value I get from its class which is formed from a php script.

Code above only works if the first input has greater value then its class value (I test this code on two rows table)

<tbody>
<tr style="text-align: center; background-color: pink;" class="rek888889" id="76">
<td>1</td>
<td>ser5555778</td>
<td>test_vaccine98</td>
<td>74 / <input type="text" id="76" name="sended_cnt" class="sended_cnt rek888889 74" maxlength="3"></td>
<td>31.10.2026</td>
<td>rek888889</td>
<td><input class="send_vacc checkbox" id="76" type="checkbox"></td>
</tr>
<tr style="text-align: center; background-color: pink;" class="rek123" id="1">
<td>2</td>
<td>ser1098</td>
<td>test_vaccine_1</td>
<td>600 / <input type="text" id="1" name="sended_cnt" class="sended_cnt rek123 600" maxlength="3"></td>
<td>13.10.2025</td>
<td>rek123</td>
<td><input class="send_vacc checkbox" id="1" type="checkbox"></td>
</tr>
</tbody>


How can I change this code to check all the values?

Answer

You can't have same ID more than once in your code. So checkbox and input IDs must be different. I renamed input IDs to inputXX . Also you could write your selectors in much better ways.

fiddle

https://jsfiddle.net/ergec/juney6mq/

snippet

$("#btn_send_order").on("click", function() {
    var keys = new Array(),
        array = new Array();
    $('.send_vacc.checkbox:checked').each(function() {
        if (parseInt($("input[id='input" + this.id + "'].sended_cnt").val()) > parseInt($("input[id='input" + this.id + "'].sended_cnt").attr("class").split(" ")[2])) {
            return false;
        } else {
            array.push($("input[id='input" + this.id + "'].sended_cnt").val());
            keys.push(this.id);
        }
    });
	console.log(keys);
	console.log(array);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table>
    <tbody>
        <tr style="text-align: center; background-color: pink;" class="rek888889" id="76">
            <td>1</td>
            <td>ser5555778</td>
            <td>test_vaccine98</td>
            <td>74 /
                <input type="text" id="input76" name="sended_cnt" class="sended_cnt rek888889 74" maxlength="3">
            </td>
            <td>31.10.2026</td>
            <td>rek888889</td>
            <td>
                <input class="send_vacc checkbox" id="76" type="checkbox">
            </td>
        </tr>
        <tr style="text-align: center; background-color: pink;" class="rek123" id="1">
            <td>2</td>
            <td>ser1098</td>
            <td>test_vaccine_1</td>
            <td>600 /
                <input type="text" id="input1" name="sended_cnt" class="sended_cnt rek123 600" maxlength="3">
            </td>
            <td>13.10.2025</td>
            <td>rek123</td>
            <td>
                <input class="send_vacc checkbox" id="1" type="checkbox">
            </td>
        </tr>
    </tbody>
</table>
<button id="btn_send_order">Send Order
</button>

Comments