JQUERY function not working

aguisa
aguisa used Ask the Experts™
on
Hi. I have the following code in my app. The thing is that it is not working as expected. I have several input fields that are identified by id = hord1, hord2 ...., hordn. What I want is to validate every one of them on blur.  Thanks.

var nrows = 0;
$(document).ready(function(){
    var xj = 1;
    updaterows();
    while (xj <= nrows){
        $("#hord"+xj).blur(function(){
            alert (xj);
            validarord(xj);
        });
        xj++;
    }
});
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
leakim971Multitechnician
Top Expert 2014

Commented:
You get what in the alert with the following :
var nrows = 0;
$(document).ready(function(){
    var xj = 1;
    updaterows();
alert( "number of row : " +nrows);
    while (xj <= nrows){
        $("#hord"+xj).blur(function(){
            //alert (xj);
            validarord(xj);
        });
        xj++;
    }
});

Open in new window

Author

Commented:
Hi, updaterows() updates the value of nrows, which throw 32 at my context. Thanks for the fast response.
leakim971Multitechnician
Top Expert 2014

Commented:
if updaterows(); use ajax, you may to use :
http://api.jquery.com/attribute-starts-with-selector/
and :
http://api.jquery.com/live/
$(document).ready(function(){
    var xj = 1;
    updaterows();
    $("input[id^='hord'"]).live("blur", function(){
         alert (xj);
         validarord(xj);
    });
});

Open in new window

Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

leakim971Multitechnician
Top Expert 2014

Commented:
>Hi, updaterows() updates the value of nrows, which throw 32 at my context. Thanks for the fast response.

Yes, but you did not answer to my question. Line 5 we placed an alert which should give the number of row. Confirm you get 0
try something like this

$('input:text[id^=hord]').blur(function() {

Open in new window

that should if formed right get you every input field with an id that starts with hord.
Commented:
I'll give it a try. At the moment I solved this problem by adding the onblur clause in the input type definition.

Thanks

Author

Commented:
I couldn't follow up what you suggested. Thanks anyway.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial