0

I have modal jQuery UI dialog with a checkbox:

            $dialog = $('<div id="formContainer"></div>')
                        .html('<div>some text</div><input id="accept_cb" type="checkbox" checked="checked"/> Uncheck this box to disable.<br />')
                        .dialog({
                                autoOpen: false,
                                title: 'Title ',
                                modal: true,
                                buttons: {
                                    "Close": function() {
                                        checkboxHandler();
                                    },
                                    Cancel: function() {
                                        $( this ).dialog( "close" );
                                    }
                                }
                            });


            function checkboxHandler(){
                if ($('#accept_cb').is(':checked'))
                {
                  alert('checked');
                }else{
                  alert('not checked');
                }
            }

When I open the dialog the first time everything works just fine and alerts the correct checked status. But when I return a second time the status stays either 'checked' or 'not checked' whatever it was the first time. What do I need to change?

I also tried with ().attr and ().prop, same result.

1
  • 1
    Missing quote : .html(<div>some text Commented May 2, 2012 at 6:49

3 Answers 3

1

Closing a dialogue does not destroy it. Although you don't mention how you are opening the dialogue, I imagine that you are just running this code again. If so you are creating duplicates of all these elements. When you have 2 elements with the same ID on a page, your browser will always return the first one, hence your "whatever it was the first time".

Either .remove() the dialogue in the "close" handler, or don't re-create the contents each time.

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

1 Comment

This has solved it, I was removing $dialog instead of $('#formContainer'). Thanks.
1

In your code i see a syntax error. Pay attention on your '. Also see jQuery API for more information about checkboxes http://api.jquery.com/checked-selector/

Comments

1

Simple solution. You need to use class for checkbox and use selector with :last, because ui modal dialog made duplicate of this element.

function checkboxHandler(){
   if ($('.accept_cb:last').is(':checked'))
   {
      alert('checked');
   }else{
      alert('not checked');
   }
}

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.