0

I am trying to display a dialog box when users access a page with a device smaller than a desktop. I am not an expert in jQuery. I tried the following below but was not successful.

$(document).ready(function () {
  if (window.matchMedia('(max-width: 767px)').matches) {
    var dialog = $("#ScreenSize").dialog({
      modal: true,
      autoopen: true,
    }).show();
  } else {
    $("ScreenSize").dialog().hide();
  }
});
<div id="ScreenSize" style="display:none">
  <p>Go to Text Box</p>
</div>
2
  • You have a typo. The # is missing before ScreenSize for the hide() Commented May 24, 2017 at 10:36
  • @Gerard thanks for spotting it but it still not working Commented May 24, 2017 at 10:42

4 Answers 4

1

This code is executed on Dom ready. Considering that you open that dialog only in the if branch, actually the else branch is useless because when you reload the page, it doesn't get opened at all. Or maybe there is some other logic that you are not providing.

Then .dialog() is a method of the jQuery UI library. You should add also that library in order to get the dialog working. If you open your console you see that the method dialog() is undefined.

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

1 Comment

This is correct. I've just demonstrated it in my answer.
1

Your javascript code is OK. On the html part, jQuery and jQuery UI (for the dialog widget) scripts must be loaded in the correct order: jQuery first, UI after. Try the following code snippet, it works as you wanted.

$(document).ready(function () {
  if (window.matchMedia('(max-width: 767px)').matches) {
    var dialog = $("#ScreenSize").dialog({
      modal: true,
      autoopen: true
    }).show();
  } else {
    // this part is useless...
    $("ScreenSize").dialog().hide();
  }
});
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <link href="https://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/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>  
</head>
<body>
  <div id="ScreenSize" style="display:none">
    <p>Go to Text Box</p>
  </div>
</body>
</html>

2 Comments

Although, a css based solution (something like @Jason answered) could be better, and then with jQuery you can test it like if $('#ScreenSize').is(':visible') is true then open your modal...
thanks i have sorted it out myself but i also test your solution and it worked i appreciate it.
0

Why is jQuery needed? Why not just use css media query to display and hide your dialog box.

CSS:

#ScreenSize {
  display: none;
}

@media only screen and (max-width: 767px) {
    #ScreenSize {
        display: block;
    }
}

Comments

0

i have used these following below in order to find a solution to my problem

    $(function () {
    if (screen.width < 1023) {
        $("#ScreenSize").show();


        var dialog = $("#ScreenSize").dialog({

            modal: true,
            autoopen: true,
            resizable: false, draggable: false,



        })


    }
    else {
        $("#ScreenSize").hide();
    }


});

<span class="ui-state-default ui-corner-all" style="float: left; margin: 0 7px 0 0;"><span class="ui-icon ui-icon-info" style="float: left;"></span></span>

    <div style="margin-left: 23px;">


     <p>go to Text Box</p>
 </div>
                     <//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.