0

How can I do that ? This is not working, no results, no effect, no error output.

$('input[type=checkbox]').each(function() 
    {
            if($(this).attr("checked") === true){
            $('.hoverbox .a').css('text-transform', 'uppercase');
            }
            else
            {
            $('.hoverbox .a').css('text-transform', 'lowercase');
            }
    });
noneJavaScript
  • 835
  • 3
  • 21
  • 41
  • You need to clarify exactly what you're trying to accomplish. – Blazemonger Feb 25 '13 at 16:35
  • Do NOT completely change your original question on an edit. Submit a new question if you have a new problem. – Blazemonger Feb 25 '13 at 16:46
  • I didn't, its the same problem. – noneJavaScript Feb 25 '13 at 16:46
  • It is not the same problem. Your original source code didn't have an `.on('change', ...)` in it. Worse, you've added the completely useless statement "it won't work" without explaining what "it" is or what "working" looks like. – Blazemonger Feb 25 '13 at 16:47
  • I can't uderstand why it is so hard to figure it out what I pretend to achieve. If checked checkbox then uppercase that div class else lower case it. – noneJavaScript Feb 25 '13 at 16:51
  • It's hard to figure it out because you never explained it in the question. Thank you for finally clarifying; I've updated my answer. – Blazemonger Feb 25 '13 at 16:58

2 Answers2

5

You probably need an event handler:

$('input[type=checkbox]').on('change',function(e) {
   // do stuff
});

http://api.jquery.com/on

Or possibly (it's hard for me to understand your intentions) you need to use .prop() instead of .attr():

if ($(this).prop('checked')) {
    // do something 
} else {
    // do something else
}

update: Putting it all together:

$('input[type=checkbox]').on('change',function(e) {
    if ($(this).prop('checked')) {
        $('.hoverbox .a').css('text-transform', 'uppercase');
    } else {
        $('.hoverbox .a').css('text-transform', 'lowercase');
    };
});

Although adding a custom class and toggling it would save you a little trouble:

$('input[type=checkbox]').on('change',function(e) {
    $('.hoverbox .a').toggleClass('uppercase');
});

CSS:

.hoverbox .a {
    text-transform: lowercase;
}
.hoverbox .a.uppercase {
    text-transform: uppercase;
}
Blazemonger
  • 90,923
  • 26
  • 142
  • 180
2

Why cant you use CSS? Why waste javascript?

input[type=checkbox]:checked + div {
    text-decoration:underline;
}
PlantTheIdea
  • 16,061
  • 5
  • 35
  • 40