0

I have a JavaScript alert that is working perfectly for what I want it to do by returning an alert by clicking on # menu2, however I want to change the visual part of the alert to work perfectly in the Chrome browser. For this I tried several times the use of jQuery, Bootbox among others. So I've tried a better programming here to make my script more user-friendly.

I've tried using, but to no avail for me.

http://jqueryui.com/dialog/

http://bootboxjs.com/

Among other options of a previous question:

how to change the style of alert box

Here's my html

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>

<ul id='menu'>
  <li id='menu1'><a href='/web/front/helpdesk.public.php' title="Home" class='itemP'>Home</a>
  </li>
  <li id='menu2'><a href='/web/front/helpdesk.public.php' title="Cria um chamado" class='itemP'>Cria um chamado</a>
  </li>
  <li id='menu3'><a href='/web/front/ticket.php' title="Chamados" class='itemP'>Chamados</a>
  </li>
  <li id='menu4'><a href='/web/front/reservationitem.php' title="Reservas" class='itemP'>Reservas</a>
  </li>
  <li id='menu5'><a href='/web/front/helpdesk.faq.php' title="FAQ" class='itemP'>FAQ</a>
  </li>
</ul>

And here's my javascript

 "<script type='text/javascript'>
           $(document).ready(function(){
          $('#menu2').click(function(event){
          event.preventDefault();
   
          alert('Text');
            })
            });
            </script>";

My alert on Chrome

enter image description here

What I tried to do to change the visual part of my alert.

"<script type='text/javascript'>
            $(document).ready(function () {
            $('#menu2').dialog({
                e.preventDefault();
            });
            $('#menu2').click(function () {
            });
        });
         </script>";

I can not program the examples described here in my php code. So I posted part of my code where I have my script working. I ask you to use the code for the solutions described here to improve my understanding.

if (Session::haveRight("ticket", CREATE)) {
         $menu['create_ticket']['id']= "menu2";
             /*My alert of menu2*/
         echo "<script type='text/javascript'>
           $(document).ready(function(){
          $('#menu2').click(function(event){
          event.preventDefault();
   
          alert('Text');
            })
            });
            </script>";
         $menu['create_ticket']['default'] = '/front/helpdesk.public.php?create_ticket=1';
         $menu['create_ticket']['title']   = __s('Create a ticket');
         $menu['create_ticket']['content'] = array(true);
}

Obs: I am issuing the script to a PHP application.

I hope someone can help me ..

I can program the alert for a better look by using the sweetalert. Now I need to call the onClick function for #menu. But I can not move forward.

My new script

echo '<script type="text/javascript">';
echo 'setTimeout(function () { swal("Return the alert test !","Message!","success");';
echo '}, 1000);</script>';

My new alert:

enter image description here

Community
  • 1
  • 1
Renato Lazaro
  • 177
  • 2
  • 10
  • Not quite understanding why you said jQueryUI Dialog didn't work for you. You could style the dialog the way you want to as it's purely made of just normal HTML, whereas the Alert function uses the system default style to render the dialog box, which is hard to customise with a different style. – woodykiddy Nov 26 '16 at 14:35
  • I am a beginner in programming and I can not put my script using the examples above. It does not return the alert when I click on the menu. It may be a simple thing, but I do not understand how to do it. – Renato Lazaro Nov 26 '16 at 14:39
  • Are there any errors in your console? When coding javacript, be sure to open your browser's developer console and learn to use that tool - it's super powerful. – random_user_name Nov 26 '16 at 14:47
  • It does not return error in my javascript looking console, my script works perfectly returning alert when clicking on menu2, but I want to try to use another option to change the visual part of the alert. That's why I've come to you for help here. – Renato Lazaro Nov 26 '16 at 14:52

3 Answers3

2

You need to get a basic understanding of how javascript works, and what you are looking at.

First, you can NOT control the look / feel of an alert. Those are built-in browser functions.

So, to manage the way it looks, you are on the right track to use something like jQuery UI Dialog.

To use the jQuery UI Dialog (https://jqueryui.com/dialog), you call .dialog on the element, like so:

$('#my-dialog').dialog();

In your case, I suspect you want to use it as a "modal confirmation", so you should read these docs: https://jqueryui.com/dialog/#modal-confirmation

To use it that way, you trigger it by passing in certain arguments:

$( "#my-dialog" ).dialog({
  resizable: false,
  height: "auto",
  width: 400,
  modal: true,
  buttons: {
    "Confirm": function() {
      // ** DO THE THINGS YOU WANT when they confirm "Yes" here.
      $( this ).dialog( "close" );
    },
    Cancel: function() {
      $( this ).dialog( "close" );
    }
  }
});

Those arguments are in the structure of a javascript object, with name-value pairs of different options. For example, above resizable is an option name, and false is the value. Be sure to check out the dialog API information to know all of the options, methods, etc.

So, modify the code from your question (copied here):

<script type='text/javascript'>
        $(document).ready(function () {
        $('#menu2').dialog({
            e.preventDefault();
        });
        $('#menu2').click(function () {
        });
    });
     </script>

To be something like this:

<script>
    jQuery(function ($) {
        $('#menu2').click(function () {
            $( "#dialog-confirm" ).dialog({
              resizable: false,
              height: "auto",
              width: 400,
              modal: true,
              buttons: {
                "Confirm": function() {
                      // ** DO THE THINGS YOU WANT when they confirm "Yes" here.
                      $( this ).dialog( "close" );
                },
                Cancel: function() {
                      $( this ).dialog( "close" );
                }
              }
            });
        });
    });
</script>

And be sure to have the markup on your page to support this dialog. For example, place something like this just before the closing </body> tag:

<div id="dialog-confirm" title="Are you sure you want to do this?">
  <p><span class="ui-icon ui-icon-alert" style="float:left; margin:12px 12px 20px 0;"></span>You are about to do something. Are you sure?</p>
</div>
random_user_name
  • 25,694
  • 7
  • 76
  • 115
  • cale! I tried to summize your example in my www, but I have no return from the alert. the example I created in github. https://gist.github.com/Nerato/f024a636fbbc6e23d1ff95714ea1b2aa – Renato Lazaro Nov 26 '16 at 15:52
-1

Use Sweet alert Jquery Plugin for highly customized alert

Ghanshyam Singh
  • 1,361
  • 2
  • 15
  • 27
  • Can you explain me better? – Renato Lazaro Nov 26 '16 at 19:23
  • For better understanding please check visit this link http://t4t5.github.io/sweetalert/ – Ghanshyam Singh Nov 26 '16 at 19:28
  • Look at my source code and see if you can help me, I'm trying some time, but without success...https://gist.github.com/Nerato/bd7a34eab929db61958c75b9f850d101 – Renato Lazaro Nov 26 '16 at 19:34
  • Click the link and Download SweetAlert.zip -> extract zipeetalert-master\dist you will get sweetalert.js and sweetAlert.css file inclulde in your We page then just Write " swal("Here's a message!")" – Ghanshyam Singh Nov 26 '16 at 19:44
  • Sample code $('#menu2').click(function(event){ swal("Text Here");)}; – Ghanshyam Singh Nov 26 '16 at 19:47
  • To get this done the alert is returning, now as called the function for my menu2, I can not, using the example that passed me? echo ''; – Renato Lazaro Nov 27 '16 at 03:40
-1

My codes are javascript codes to create a client side custom alert. This is completed:

<style>
    #alertContainer {
        position: fixed;
        width: 100vw;
        height: 100vh;
        z-index: 999;
        background-color: rgba(0, 0, 0, 0.5);
        display: none;
    }
    #alert {
        width: 400px;
        top: 0px;
        right: 0px;
        bottom: 0px;
        left: 0px;
        position: absolute;
        margin: auto;
        background-color: rgb(173, 173, 173);
        padding: 30px;
        max-width: 100vw;
        height: 130px;
    }
    #ok {
        position: absolute;
        right: 30px;
        bottom: 30px;
    }
</style>

Html must copy after body start tag:

<div id="alertContainer">
    <div id="alert">
        <span id="alertText"> Hello</span>
        <input id="ok" value="ok" onclick="$('#alertContainer').hide();" type="button">
    </div>
</div>

And this is MyAlert function to use insted of javascript alert:

 function MyAlert(text)
    {
        document.getElementById('alertContainer').style.display = "block";
        document.getElementById('alertText').innerHTML = text;
    }

and this is a sample:

<a onclick="MyAlert('How are you')">alert</a>
Farzin Kanzi
  • 3,380
  • 2
  • 21
  • 23