4b0
4b0

Reputation: 22323

Show hide div through checkbox in JQuery

JQuery:

$('#chkCaption').live("click", function() {
    if ($('#chkCaption').attr("checked")) {
        $('#divCaption').show();
    }
    else {
        $('#divCaption').hide();
    }
});

But it is not work.Help to find my mistake or suggest the way.Thanks.

Upvotes: 1

Views: 8614

Answers (7)

Braiam
Braiam

Reputation: 4496

In jQuery 1.9 .live() was removed. So the actual answers changed, if using javascript .checked:

$('#chkCaption').on("click", function() {
  if (this.checked) {
    $('#divCaption').show("table-row");
  }
  else {
    $('#divCaption').hide();
  }
});

In pure jQuery:

$('#chkCaption').on("click", function() {
  if ($(this).is(":checked")) {
    $('#divCaption').show();
  }
  else {
    $('#divCaption').hide();
  }
});

Upvotes: 1

xkeshav
xkeshav

Reputation: 54022

you are missing (), TRY

$('#chkCaption').live("click", function() {
    if ($('#chkCaption').prop("checked")) {
        $('#divCaption').show();
    }
    else {
        $('#divCaption').hide();
    }
});

DEMO

Upvotes: 0

Mr.T.K
Mr.T.K

Reputation: 2356

$('#chkCaption').live("click", function() {
    if ($('#chkCaption').is(":checked")) {
        $('#divCaption').show();
    }
    else {
        $('#divCaption').hide();
    }
});

Upvotes: -2

Teun Pronk
Teun Pronk

Reputation: 1399

$('#chkCaption').click(function () {

        if ($(this).is(':checked')) {

            //Show div
        } else {
            //Hide div
        }
});

That should do it ;)

Upvotes: 0

Rylab
Rylab

Reputation: 1295

You just forgot the function declaration parens on show()

$('#chkCaption').live("click", function() {
    if ($('#chkCaption').attr("checked")) {
        $('#divCaption').show();
    }
    else {
        $('#divCaption').hide();
    }
});

Upvotes: 1

Richard Neil Ilagan
Richard Neil Ilagan

Reputation: 14747

Instead of

$('#chkCaption').attr('checked')

you may want to try this:

$(this).is(':checked')

Upvotes: 5

Kamil Lach
Kamil Lach

Reputation: 4629

try this:

$('#chkCaption').live("click", function() {
    if (this.checked) {
        $('#divCaption').show();
    }
    else {
        $('#divCaption').hide();
    }
});

this should be in document.ready.

Instead of Jquery attr or is you can use javascript property checked

Upvotes: 2

Related Questions