views:

391

answers:

3

I can't seem to get this to work. If I remove the checkbox portion of the script the toggle class works but if I add it, then it no longer toggle's the class. I'm a total amateur at this so I would greatly appreciate any help. I'm using jQuery version 1.4.1. Thanks.

$("li.imprint").click(function() {
  $(this,"li.imprint").toggleClass('selected').children("input[@type=checkbox]")[0].click();
});

So essentially I'm building a web form for people to customize pens. So they need to choose what color imprint they want. Rather than just a boring list of colors, I wanted to show a swatch of the color. So I figured I would create a list, put some checkboxes so they can select the colors and use CSS to hide the actual checkbox so it's all nice and clean. I saw the code to accomplish this for radio buttons and I got that working fine. I tried to adapt it to checkboxes but the problem was that you can only select one radio button but multiple checkboxes.

To give you a semi-function example. If you head to the beta site, it's live (not a good practice I know) and try to customize a pen you can see what I'm trying to do. Pensfast.com Beta

A: 

try:

$("li.imprint").click(function() {
  $(this,"li.imprint").toggleClass('selected')
        .children("input:checkbox:first").attr('checked',true);
});

may I ask why you want to call 'click' on the checkbox?

if you want to check or uncheck checkboxes, read this.

update

try this:

$("li.imprint").click(function() {
      $(this).toggleClass('selected')
      $(':checkbox',this).attr('checked',$(this).is('selected'));
 });
Reigel
That did not work. The toggleClass portion worked but the checkbox did not check.
Jitpal
I didn't notice your question earlier. I'm building a form where the user can select various colors. So I'm using CSS to hide the checkbox and showing an image. If the user selects the list item, the checkbox gets selected and I can get the formdata.
Jitpal
try `.attr()` look above for the update.
Reigel
I tried that and it worked! However, when the user tries to deselect, the class toggles off but the checkbox remains checked.
Jitpal
use false to uncheck..
Reigel
nope ... `removeAttr('checked')` would be correct. also ... `attr('checked', 'checked)` would be correct!
Andreas Niedermair
I'm a little confused. How can I have it so the user toggles the checkbox status by clicking? If I set it to true or if I set it to false then only one can happen. What would be the way around that?
Jitpal
can you please add more details in your question... just edit your question above... I'm a little confused also on what you wanted. ;)
Reigel
this is neither xhtml-valid nor does `this` represent the checkbox
Andreas Niedermair
I appreciate the help. Thanks.
Jitpal
@Andreas sorry, I forgot the `:checkbox`... @Jitpal try my update above..
Reigel
still no valid xhtml ..
Andreas Niedermair
A: 
$('li.imprint').click(function() {
    var element = $(this);
    var checkbox = $(':checkbox', element); // correct if necessary!

    element.toggleClass('selected');
    if (element.is('.selected')) {
        checkbox.attr('checked', 'checked');
    }
    else {
        checkbox.removeAttr('checked');
    }
});
Andreas Niedermair
I tried this but it did not check the box.
Jitpal
then your selector of the checkbox is wrong ... will update
Andreas Niedermair
I appreciate the help. Thanks
Jitpal
you're welcome!
Andreas Niedermair
A: 

Given your updated details, this is what you want:

$("li.imprint").click(function(){
    var $checkbox = $(this).toggleClass('selected').find(':checkbox');
    if($(this).hasClass('selected')) {
      $checkbox.attr('checked','checked');
    } else {
      $checkbox.removeAttr('checked');
    }
})

I am concerned that your classes are incorrect. You reference li.imprint in your question, but the page I visiting on your site had the class of li.Barellimprint Obviously, this answer won't work if the selectors are wrong. Try changing the first line to :

$("li.Barrelimprint").click(function(){

And this code would work on this page.

Doug Neiner
That did it. Thank you so much!
Jitpal
@Jitpal Glad it worked!
Doug Neiner
I simplified the selector, it's actually building the form through php loops because each pen is different. Some have multiple color options, some have multiple imprinting locations, then colors per location. It's a pain in the neck really.
Jitpal
@Jitpal ... Ah, makes sense. Glad its working for you. The site looks really nice by the way!
Doug Neiner
Thanks, I appreciate it.
Jitpal