2

I have the following HTML code:

<div class="member">
    <input type="checkbox" value="64" style="display:none;" name="memberId[]" />
    <div class="memberInfo">John Doe</div>
</div>

I'm trying to get a jQuery script that when a visitor presses the member div it will change the background color, and check the memberId checkbox.

How should such a jQuery script look like?

Thanks,

3 Answers 3

3

You should probably do this with toggle which allows you to set two handlers: one for the first time a handler is triggered and then one for the next time, repeating as necessary.

$('.member').toggle(function() { // first time
    $(this).css('background-color', '#ff0000')
           .find('input:checkbox').attr('checked', true);
}, function() { // second time
    $(this).css('background-color', '#ffffff')
           .find('input:checkbox').attr('checked', false);
});
Sign up to request clarification or add additional context in comments.

4 Comments

Although this should work as I see it works on jsfiddle, it doesn't work on my page for some reason. my member div is inside a jquery dialog, does that make any difference?
@Or W It shouldn't matter. Could you post your actual code somewhere accessible?
Fixed it by wrapping your function with jQuery(document).ready(function (){
@Or W Excellent -- I was taking that as read! Sorry for not being more explicit.
3
<script type="text/javascript">
    jQuery(document).ready(function (){
        $(".member").click(function() {
            $("input[name=memberId]").attr('checked', true);
            $(this).css('background-color','#F00');
        });

    });
</script>

Edit: The others were way faster than me :(

Comments

0
$('.member').click(function(){
    $(this).css('background-color','#ABC098');
    $(this).find("input[type='checkbox']").attr('checked',true)
});

1 Comment

Although this should work as I see it works on jsfiddle, it doesn't work on my page for some reason. my member div is inside a jquery dialog, does that make any difference?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.