jquery Tree Traversal prev() problem.

Posted by Guido Lemmens on Stack Overflow See other posts from Stack Overflow or by Guido Lemmens
Published on 2010-03-25T00:27:45Z Indexed on 2010/03/25 0:33 UTC
Read the original article Hit count: 411

Filed under:
|

Hello,

I like to click a label and check the previous checkbox. I've tried the next code, but this is not working. I have tried for 2 hours, but what am i missing?

JQUERY

jQuery(document).ready(function() {
    $('.profilename').live('click', function() {
        if ($(this).prev("input:checked").val() != null) {
            $(this).prev("input").removeAttr("checked");
        } else  {
            $(this).prev("input").attr("checked","checked"); 
        }
    });
});

HTML
<input type="checkbox" class="check" name="example"> <label class="namelabel">John Doe</label>

Who can help me to solve this question? Many thanks!

p.s. I know i can easy solve this with the <label for=""> tag, but that is not the question.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about tree-traversal