1

My site pages run off header.php, which contains all the JavaScript and CSS needed for all the pages.

What I am trying to do is make it so that, whatever page a user is on, if they click the <div> .register_button_click, they will have a JavaScript dialogue box open up. I have placed the JavaScript in the header.php file and have created my <div class="register_button_click">click here</div>, but it's not bringing anything up.

Where am I going wrong?

<head>       
 <script type="text/javascript">
        var $dialog; 
        $(document).ready(function () {
            $dialog = $('<div></div>')
            .html('My Dialog Demo...')
            .dialog({
                autoOpen: false,
                title: 'My Dialog'
            });

            $('#register_button_click').click(function () {
                $dialog.dialog('open');
                return false; ////cancel eventbubbeling
            });
        });

        function showDialog() {
            $dialog.dialog('open');
            return false //cancel eventbubbeling
        }

    </script>
 </head>
6
  • 1
    Your question says .register_button_click but the code says #register... and you have a showDialog method that is never called. Jan 28, 2013 at 15:18
  • access elements through class by $('.className') and not otherwise Jan 28, 2013 at 15:18
  • Are you are creating the div dinamically? in that case the event won't be binded and you need to use jQuery live or similar api.jquery.com/live
    – Mc-
    Jan 28, 2013 at 15:23
  • 2
    @Mc- Don't point people to live as it is deprecated. Point them to the "event delegation" section of the on documentation. Jan 28, 2013 at 15:36
  • Wops, totally right @JamesMontagne
    – Mc-
    Jan 28, 2013 at 16:20

2 Answers 2

1

Change from an id to a class:

           $('.register_button_click').click(function () {
               showDialog();
            });
1

In the jquery function you are trying to access the element by it's ID. But you only have defined the element's class in the HTML code.

Change your div to <div id="register_button_click">click here</div> and everything is gonna be ok.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.