jQuery dialog popup
Asked Answered
Q

6

27

I am trying to get this dialog popup form to show up when this link is clicked but it does not work for me. I've been working on this for the past three hours and this is getting too frustrating for me.

Here's my HTML:

<a href="#" id="contactUs">Contact Us</a>
<div id="dialog" title="Contact form">
  <p>appear now</p>
</div>

And here's my JavaScript, this is in an external file:

$("#contactUs").click(function() {
  $("#dialog").dialog("open");
  return false;
});

I've used these links, but to no avail for me:

Please let me know if have an ideas, much appreciated, thanks.

Quinones answered 3/6, 2012 at 0:50 Comment(7)
No error? No weird visual actions? Nothing ?Anastice
If you're interested, I have a plugin for making jQuery dialogs easier: mostthingsweb.com/2011/12/dialogwrapper-2-1-releasedOmaromara
ah sorry, basically, the error is that the popup isn't showing up at all. And thanks for the link, I'll check that out now.Quinones
How about a demo on jsfiddle.net? The answers below should be working so there is something going on that you're not telling us about.Elburt
That's a pretty cool website, thanks for sharing it. In anycase, here's my link: jsfiddle.net/CXgv9Quinones
I know this is a bit old, but your code doesn't reference the jQuery UI js and css files. Also, you are missing the navBar element that your code references to create the menu.Sidsida
I had this same problem, and it turned out I was importing two different jquery versions. When I removed the second everything worked as expected. Perhaps this may help someoneSt
C
51

This HTML is fine:

<a href="#" id="contactUs">Contact Us</a>                   
<div id="dialog" title="Contact form">
  <p>appear now</p>
</div>

You need to initialize the Dialog (not sure if you are doing this):

$(function() {
  // this initializes the dialog (and uses some common options that I do)
  $("#dialog").dialog({
    autoOpen : false, modal : true, show : "blind", hide : "blind"
  });

  // next add the onclick handler
  $("#contactUs").click(function() {
    $("#dialog").dialog("open");
    return false;
  });
});
Cavitation answered 3/6, 2012 at 0:55 Comment(1)
Thanks, this helps, however, the dialog still doesn't show up for me. I'll try looking around more.Quinones
S
8

Your problem is on the call for the dialog

If you dont initialize the dialog, you don't have to pass "open" for it to show:

$("#dialog").dialog();

Also, this code needs to be on a $(document).ready(); function or be below the elements for it to work.

Sidsida answered 3/6, 2012 at 0:57 Comment(1)
Thanks. I tried it without passing the open, but still does not work for me. Also, I didn't post the ready event, but it's present in the document.Quinones
P
7

Use below Code, It worked for me.

<script type="text/javascript">
     $(document).ready(function () {
            $('#dialog').dialog({
                autoOpen: false,
                title: 'Basic Dialog'
            });
            $('#contactUs').click(function () {
                $('#dialog').dialog('open');
            });
        });
</script>
Pownall answered 11/1, 2015 at 19:18 Comment(0)
S
5

It's quite simple, first HTML must be added:

<div id="dialog"></div>

Then, it must be initialized:

<script type="text/javascript">
  jQuery( document ).ready( function() {
    jQuery( '#dialog' ).dialog( { 'autoOpen': false } );
  });
</script>

After this you can show it by code:

jQuery( '#dialog' ).dialog( 'open' );
Sedda answered 8/12, 2013 at 10:5 Comment(0)
J
3

You can use the following script. It worked for me

The modal itself consists of a main modal container, a header, a body, and a footer. The footer contains the actions, which in this case is the OK button, the header holds the title and the close button, and the body contains the modal content.

 $(function () {
        modalPosition();
        $(window).resize(function () {
            modalPosition();
        });
        $('.openModal').click(function (e) {
            $('.modal, .modal-backdrop').fadeIn('fast');
            e.preventDefault();
        });
        $('.close-modal').click(function (e) {
            $('.modal, .modal-backdrop').fadeOut('fast');
        });
    });
    function modalPosition() {
        var width = $('.modal').width();
        var pageWidth = $(window).width();
        var x = (pageWidth / 2) - (width / 2);
        $('.modal').css({ left: x + "px" });
    }

Refer:- Modal popup using jquery in asp.net

Jayme answered 30/3, 2015 at 4:41 Comment(0)
T
0

You can check this link: http://jqueryui.com/dialog/

This code should work fine

$("#dialog").dialog();
Toreutic answered 3/9, 2013 at 11:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.