1

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>
1
  • if (showTips.checked = true) should just be if (showTips.checked), but where does showTips come from? Commented Sep 29, 2012 at 13:26

4 Answers 4

3

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!!!');
     }
}

http://jsfiddle.net/n74JG/

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

Comments

2

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>

Comments

1

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!!!');
        }
    }

Comments

0

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.

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.