Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

My javascript and jquery not working in jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
  <script type="javascript" src="despage/javascript/script.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script type="text/javascript">
    function alert() {
      alert("hello");
    }
    $(document).ready(function() {
      ("p").click(function() {
        $(this).hide();
      });
    });
  </script>
  <meta charset="ISO-8859-1">
  <title>home</title>
</head>
<body>
  <h1 style="text-align:center;font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;font-weight: bolder;color:rgb(222, 222, 222);">HEAD</h1></div>
  <p onclick="alert()">this one is for test</p>
</body>

I used cdn url for jquery it didnt work ,network connection was stable.simple alert() function onclick on the p tag was working until i wrote that alert function call in the script tag

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You’ve created an infinitely recursive function here:

function alert() {
  alert("hello");
}

When you call alert(), it internally calls alert(), which internally calls alert(), which internally calls alert(), and so on forever.

You don’t need to write an alert function in the browser. It already has one. Just remove this function of yours entirely.


Additionally, a string has no function called click:

("p").click(/*...*/)

You’re probably looking to use that string as a jQuery selector:

$("p").click(/*...*/)

Also, don’t use inline click handlers:

<p onclick="alert()">

Remove the handler from there:

<p>

And, since you’re using jQuery, just use jQuery to attach a click handler:

$("p").click(/*...*/)

Overall, it sounds like the click handler you want is simply:

$("p").click(function() {   
  $(this).hide();
  alert("hello");
});

(Which should, in this structure, still remain inside your document‘s ready handler.)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading