-1

can anyone help me? i've got a silly question. i try to make an alert function using js but it doesnt execute

<script type="text/javascript">
  $(".delete").click(function(){
    alert("Are you sure?");
  });
</script>

and this is my delete button

<td><a class=\"btn delete btn-danger\" href='".base_url().'admin/blog/deleteblog/1'."'>Delete</a></td>
ADyson
  • 57,178
  • 14
  • 51
  • 63

2 Answers2

0

I'm unsure why you have used the \ around your class name here, but one is within the quotes and one outside. I would remove these.

"btn delete btn-danger"

 <td>
    <a class="btn delete btn-danger" href='".base_url().'admin/blog/deleteblog/1'."'>
       Delete
    </a>
</td>

As for your script, you should check if the document is ready before using JQuery - https://learn.jquery.com/using-jquery-core/document-ready/

<script type="text/javascript">
   $( document ).ready(function() {
        $(".delete").click(function(){
            alert("Are you sure?");
        });
   });
</script>
  • I think the quotes are fine - from the use of `.` later they appear to be using PHP and this is clearly a double-quoted string, so using `\"` to escape double-quotes that you want in the output is correct. Of course there are better and more readable ways to get this output but I see no evidence of an actual mistake here. – Robin Zigmond Sep 17 '21 at 16:21
  • Well observed, perhaps it could be second issue of the document not being ready prior to the on click. – Michael Butler Sep 17 '21 at 16:22
  • I agree that's one of the likely causes of this issue. But there's really not enough to go on in the question as posed to be sure of that. – Robin Zigmond Sep 17 '21 at 16:23
0

It seems to work fine in the example below for me.

<!DOCTYPE html>
<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  </head>
  <body>
    <td><a class="btn delete btn-danger" href="#">Delete</a></td>
  </body>
  <script>
    $(".delete").click(function () {
      alert("Are you sure?");
    });
  </script>
</html>