Reputation: 6362
I am trying to call changeShowTips() method in JQuery UI dialog, however the method always returns "NOT checked!!!!"
what seems to be the problem?
<div id="dialog-tip">
<div>
<input type="checkbox" name="showTips" value="showTips" checked="checked" onclick="changeShowTips();"/>Show tips on start up
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
showTips()
});
function showTips() {
$("#dialog-tip").dialog({
height: 520,
width: 515,
modal: true,
}).parents('.ui-dialog:eq(0)').wrap('<div class="black-tie"></div>');
}
function changeShowTips() {
if (showTips.checked == true) {
alert('checked!!!');
}
else {
alert('NOT checked!!!');
}
}
</script>
Upvotes: 1
Views: 6338
Reputation: 144689
That's because in your code showTips
refers to your function not the target element.
<input type="checkbox"
name="showTips"
value="showTips"
checked="checked"
onclick="changeShowTips(this);"
/> Show tips on start up
function changeShowTips(showTips) {
if (showTips.checked) {
alert('checked!!!');
}
else {
alert('NOT checked!!!');
}
}
Upvotes: 3
Reputation: 688
Syntax wise it looks like you are trying to use vanilla JS not jQuery to check the checkbox.
A jQuery selector looks like this $([...]) and typically you use it to select for classes $(".someclass") or ids $("#someid"). If you really don't want to give the input field an ID then take a look at the documentation: http://api.jquery.com/attribute-equals-selector/
And then you can use $([...]).prop("checked") to find out if it is checked or not.
Upvotes: 0
Reputation: 1699
You have some messy jQuery
<div id="dialog-tip">
<div>
<input type="checkbox" name="showTips" value="showTips" checked="checked"/>Show tips on start up
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
showTips()
$('input', '#dialog-tip').click(changeShowTips()); // do not have inline js, bind it here
});
function showTips() {
$("#dialog-tip").dialog({
height: 520,
width: 515,
modal: true,
}).parents('.ui-dialog').eq(0).wrap('<div class="black-tie"></div>');
// it is faster for sizzle to use a standard css selector to modify the jquery object and then use a jquery specific selector 'eq' separately
// instead of combining css/jquery selectors in one statement
}
function changeShowTips() {
if ($('input', '#dialog-tip').is(':checked')) { // there is jQuery for this - you should also compare and not assign
alert('checked!!!');
}
else {
alert('NOT checked!!!');
}
}
</script>
Upvotes: 2
Reputation: 4188
try this instead...
Give the input an id of showTips as well as the name then...
function changeShowTips() {
if ($('#showTips').is(':checked')) {
alert('checked!!!');
}
else {
alert('NOT checked!!!');
}
}
Upvotes: 1