bollo
bollo

Reputation: 1634

show jQuery textarea on checkbox tick

I am at a loose end as to why my code is not hiding when i untick. I would be grateful if someone could check my code and point out where I am going wrong. I have posted code here. many thanks

Upvotes: 0

Views: 4408

Answers (3)

Mark Coleman
Mark Coleman

Reputation: 40863

You can juse use this.checked

$('input[name=messagetick]').click(function() {
    if (this.checked) {
        $('.contactmessage').show();
    }
    else {
        $('.contactmessage').hide();
    }
});

A slightly better version would use .toggle([showOrHide])

$('input[name="messagetick"]').click(function() {
    $('.contactmessage').toggle(this.checked);
});

Side note, don't forget your " when using an attribute selector.

Example on jsfiddle

Upvotes: 5

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76900

You didn't choose jquery as framework and so mootools was loaded.

use this code:

$('input[name=messagetick]').click(function() {
      $('.contactmessage').toggle($(this).is(':checked'));     
 }); 

fiddle: http://jsfiddle.net/8k8VW/9/

Upvotes: 2

Chandu
Chandu

Reputation: 82933

You can use is(":checked") to see if the checkbox is checked.

Try this:

$('input[name=messagetick]').click(function() {
    if($(this).is(":checked")) {
    $('.contactmessage').show();     }
    else {
      $('.contactmessage').hide();
    } }); 

Example @ http://jsfiddle.net/8k8VW/18/

You have selected mootools as the JS framework. Change it to jQuery. Updated code @ [http://jsfiddle.net/8k8VW/1/][2]

Upvotes: 2

Related Questions