Reputation: 135
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
Upvotes: 1
Views: 51
Reputation:
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>
Upvotes: 1
Reputation:
Try this:
$('#hijue').change(function () {
$('#hijue').click(function() {
$('#puta').hide();
});
});
Upvotes: 0
Reputation: 4818
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>
Upvotes: 0
Reputation: 62536
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>
Upvotes: 2