1

this is my jquery

$('#hijue').change(function () {
  if($(this).prop(checked) == true) {
    $('#puta').hide();
  }
  else {
    $('#puta').show();
  }
});

and this is my html

<input type="checkbox" name="hijue" id="hijue" unchecked>
<div id="puta"></div>

This question has been posted by others before but I'm still stuck in retrieving the checkbox. Anybody please help

4
  • 2
    What is your question? Commented Nov 4, 2016 at 2:37
  • Do you have a error what is your problem that you are having? Commented Nov 4, 2016 at 2:37
  • doesn't show any output @Haza Commented Nov 4, 2016 at 2:39
  • @nzrnfourtwenty, did you check my answer? Commented Nov 4, 2016 at 2:44

4 Answers 4

2

You should use prop('checked').
Your current code uses the checked variable (which I guess you don't have).

If you open the console you should also see error regarding this.

$('#hijue').change(function () {
  if($(this).prop('checked') == true) {
    $('#puta').hide();
  }
  else {
    $('#puta').show();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="checkbox" name="hijue" id="hijue" unchecked>
<div id="puta">1234</div>

Sign up to request clarification or add additional context in comments.

3 Comments

When you say "doesn't work for you" - did you run the snippet? What exactly your code look like? Because this snippet works...
can you help me solve another question please. i'm having a headache right now haha
1

Problem is here: .prop(checked), try .is(':checked') or this.checked

You may try jQuery.toggle()

$(function() {
  $("#hijue").on('change', function() {
    $('#puta').toggle(!this.checked);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="hijue" id="hijue" unchecked>
<div id="puta">This is div</div>

Comments

0

You can use jQuery's .is(':checked') for very readable code:

$('#hijue').change(function () {
  if($(this).is(':checked')) {
    $('#puta').hide();
  }
  else {
    $('#puta').show();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="hijue" id="hijue" unchecked>
<div id="puta">asdf</div>

Comments

0

Try this:

$('#hijue').change(function () {
  $('#hijue').click(function() {
    $('#puta').hide();
  });
});

Comments

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.