Renato Lazaro Renato Lazaro - 7 days ago 7
Javascript Question

How to return alert using the Onclick event event in SweetAlert?

I'm using the sweetalert script to return the alert by clicking on menu2 on my page. So far I have made reference to my scritpts and put the alert to do its function that is to appear on the screen, however I want the alert to appear only if I click on menu2. can anybody help me?

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



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







Return of the alert working on the page.

enter image description here

I've tried it this way, but unsuccessfully so far.



echo '<script type="text/javascript">';
echo'setTimeout(function () {
echo '$(document).ready(function() {
echo '$('#menu2').click(function(event) {
swal
("Return the alert test !",
"Message!",
"success");';
echo '}, 1000);
</script>';




Answer

A mans raw jquery should look like this (properly closed functions):

$(document).ready(function(){
    $('#menu2').click(function(event){ 
        swal("Return the alert test !", "Message!")
    })
});

However he echoes it, is up to him. Also a man does not require setTimeout() by using document.ready().

Comments