Question

Im having a trouble converting my scripts to jQuery. How can I compute two elements at once?

<script type="text/javascript">
function Compute(target, currentValue){
if(parseInt(currentValue.value) == 0 || parseInt(currentValue.value) > parseInt(target.value))
{
   alert("That Value is not Valid");
}       
}
</script>

<input type="text" id="frm1" name="frm1" onkeyup="Compute(document.getElementById('frm1a'), this);"/> <br>
<input type="hidden" id="frm1a" name="frm1a" value="90"/> <br>

<input type="text" id="frm2" name="frm2" onkeyup="Compute(document.getElementById('frm2a'), this);"/> <br>
<input type="hidden" id="frm2a" name="frm2a" value="50"/> <br>

<input type="text" id="frm3" name="frm3" onkeyup="Compute(document.getElementById('frm3a'), this);"/> <br>
<input type="hidden" id="frm3a" name="frm3a" value="10"/> <br>
Was it helpful?

Solution

Something like

$(function(){
    $("input:text[name*='frm']").bind("keyup", function(){
        var currentValue = $(this);
        var target = $("#" + currentValue.attr("id") + "a");
        var currentValueVal = currentValue.val();

        if (parseInt(currentValueVal) === 0 || parseInt(currentValueVal) > parseInt(target.val()))
        {
             alert("That Value is not Valid");
        }
    });
});

See a working demo

OTHER TIPS

I can't see much of an advantage in converting this to jQuery... there is not much javascript and the markup does not benifit a huge amount from adding jQuery features and your users will have to download the jQuery library when using your site (small it is, but still...)

I could only suggest removing the document.getElement and replacing that with jQuery.

<input type="text" id="frm1" name="frm1" onkeyup="Compute($('#frm1a'), this);"/> <br>
<input type="hidden" id="frm1a" name="frm1a" value="90"/> <br>

Though I am open to being corrected

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top