Grow your CSS skills. Land your dream job.

jQuery: Traversing up, over and down

  • # September 13, 2010 at 3:09 pm

    I’ve got several forms on one page and I want to validate whether or not one of the fields on the form that is being submitted is filled in or not.

    Basically my form looks like this:









    White Shirt
    Quantity:
    Price: $29




    So my thought process was, select THIS, which would be the submit button, then use closest() to go up the tree to the table, then go back down to .quantity, and get the value of that.


    function checkform() {
    alert($(this).closest('table').children('.quantity').val());
    }

    Unfortunately, the only thing I’m getting is “undefined”. What’s the proper way to traverse to get what I’m looking for?

    # September 13, 2010 at 5:37 pm

    Try:

    function checkform() {
    alert($(this).closest('table').find('.quantity').val());
    }
    # September 15, 2010 at 8:25 am

    That didn’t work.

    I also thought about changing it to .closest(‘form’), since table is on the same level as the submit button, but that didn’t make any difference. I also tried it with the .find() that you suggested, and still no luck.

    Any other suggestions?

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".