1

In this, I try to open a dialog click on the button.

It's working well but I want to reduce this code because it looks like same on click function

$(document).ready(function() {
  $("#dialog_form_file").dialog({
    modal: true,
    autoOpen: false,
    title: "Help",
    width: 450,
    buttons: [{
        text: "OK",
        click: function() {
          $(this).dialog("close");
        }
      },
      {
        text: "Cancel",
        click: function() {
          $(this).dialog("close");
        }
      }
    ]
  });
  $("#dialog_form").dialog({
    modal: true,
    autoOpen: false,
    title: "Help",
    width: 450,
    buttons: [{
        text: "OK",
        click: function() {
          $(this).dialog("close");
        }
      },
      {
        text: "Cancel",
        click: function() {
          $(this).dialog("close");
        }
      }
    ]
  });
  $("#dialog_help").click(function() {
    $("#dialog_form_file").dialog('open');
  });
  $("#change_button").click(function() {
    $("#dialog_form").dialog('open');
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/>
<div id="dialog_form_file" style="display: none">
  <p>Hello</p>
</div>
<div id="dialog_form" style="display: none">
  <p>Hello</p>
</div>

3
  • The .dialog function of jquery ui should have a html parameter. Read the doc. Commented Aug 10, 2017 at 7:25
  • 1
    1. I added jQuery UI to your code - it was not a minimal reproducible example 2. you are hiding everything. Please update the snippet to work - add the link and button Commented Aug 10, 2017 at 7:25
  • @user5014677 You mean this doc where it states that the dialog() method has no html parameter? The OPs use of dialog() is absolutely fine here. Commented Aug 10, 2017 at 7:27

2 Answers 2

1

Here you go with a solution https://jsfiddle.net/aspoptua/2/

$(document).ready(function() {
  $("#dialog_form_file, #dialog_form").dialog({
    modal: true,
    autoOpen: false,
    title: "Help",
    width: 450,
    buttons: [{
        text: "OK",
        click: function() {
          $(this).dialog("close");
        }
      },
      {
        text: "Cancel",
        click: function() {
          $(this).dialog("close");
        }
      }
    ]
  });
  
  $("#dialog_help, #change_button").click(function() {
    $("#" + $(this).data('modal')).dialog('open');
  });
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/>
<div id="dialog_form_file" style="display: none">
  <p>Hello</p>
</div>
<div id="dialog_form" style="display: none">
  <p>Hello</p>
</div>
<button type="submit" id="dialog_help" data-modal="dialog_form_file">
Submit 1
</button>
<button type="submit" id="change_button" data-modal="dialog_form">
Submit 1
</button>

Additional code in HTML

<button type="submit" id="dialog_help" data-modal="dialog_form_file"> Submit 1</button>
<button type="submit" id="change_button" data-modal="dialog_form"> Submit 1</button>

Change of code in JavaScript

$("#dialog_help, #change_button").click(function() {
   $("#" + $(this).data('modal')).dialog('open');
});
Sign up to request clarification or add additional context in comments.

Comments

0

You can reduce your code by doing:

$("#dialog_form_file, #dialog_form").dialog({

You don't need to write same code again for 2 difference selectors.

1 Comment

i try to do this but both dialog open same place $("#dialog_help, #change_button").click(function(){ $("#dialog_form_file #dialog_form").dialog('open'); });

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.