0

I'm trying to display a "dialog" with JQuery UI when i click on a button, but it isn't displaying. Is there a problem in my program?

Here is my code:

$(function() {
  $("#mode").click(function() {
    $("#dialog").dialog("open");
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="mode" type="button" class="btn-modalita link">change mode</button>

<div id="dialog" title="Empty the recycle bin?">
  <p>
    <span class="ui-icon ui-icon-alert" style="float:left; margin:12px 12px 20px 0;">
    </span>you are changing mode. Are you sure?
  </p>
</div>

5
  • 2
    You haven't initialized your dialog. Commented Jan 26, 2018 at 20:50
  • 1
    Are you getting any errors? Did you include jQuery UI? Commented Jan 26, 2018 at 20:51
  • Yes, and I confirmed checking if(JQuery.ui) was true Commented Jan 26, 2018 at 20:53
  • Thank you for telling me, I didn't know i should have iniztialized it. Should inizialize it on button click or when the page is loaded? Commented Jan 26, 2018 at 20:54
  • you need to initialize and hide it before you actually start interacting with it. Commented Jan 26, 2018 at 20:54

3 Answers 3

3

Initialize and hide the dialog, then you're good.

$(function() {
  $("#dialog").dialog({autoOpen: false});
  
  $("#mode").click(function() {
    $("#dialog").dialog("open");
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://code.jquery.com/ui/1.12.1/themes/cupertino/jquery-ui.css" rel="stylesheet"/>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>

<button id="mode" type="button" class="btn-modalita link">change mode</button>

<div id="dialog" title="Empty the recycle bin?">
  <p>
    <span class="ui-icon ui-icon-alert" style="float:left; margin:12px 12px 20px 0;">
    </span>you are changing mode. Are you sure?
  </p>
</div>

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

6 Comments

Or initialize $("#dialog").dialog({ autoOpen: false});
yup yup. prettier that way, too. I was realizing that there was the option, just had to update. Thanks, @npearson!
Thank you very much. I'm sure it is the solution, but it didn't work to me. Should i maybe inizialize my dialog in JS before creating it in HTML?
what is it doing or not doing?
I tried to display the error and it says "TypeError: $(...).dialog is not a function". Could there be a reason for that? I'm using Chrome
|
0

I guess you are missing on there and also make sure you are using Correct version of jquery

$(function() {
  $("#mode").on('click', function(){
    $("#dialog").dialog("open");
  });
});

1 Comment

Nah, the .click() function does the same as .on("click",...)
0

I worked in Jsfiddle and it worked fine here is the code:

$( "#dialog" ).dialog({
      autoOpen: false,
      show: {
        effect: "blind",
        duration: 1000
      },
      hide: {
        effect: "explode",
        duration: 1000
      }
    });

    $( "#mode" ).on( "click", function() {
      $( "#dialog" ).dialog( "open" );
    });
<link href="https://code.jquery.com/ui/1.12.1/themes/eggplant/jquery-ui.css" rel="stylesheet"/>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<button id="mode" type="button" class="btn-modalita link">change mode</button>

<div id="dialog" title="Empty the recycle bin?">
  <p>
    <span class="ui-icon ui-icon-alert" style="float:left; margin:12px 12px 20px 0;">
    </span>you are changing mode. Are you sure?
  </p>
</div>

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.