1

I'm trying to call a function and activate an alert through it in my JSP.
This is what i've done so far:

<html>
<head>
<script type="text/javascript">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

function myFunction( test )
{
    alert( test );
}
</script>
<title>Success</title>
</head>
<body>
     <c:set var="test" scope="request" value="${requestScope.userDetails }"></c:set>
     <input type="button" id="sample_button" onclick="myFunction(${test.userName})" value="test">
</body>
</html>

What is wrong with my code?

2 Answers 2

1

You had a meta element inside script element which will throw an error while parsing the script block

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
        <script type="text/javascript">

            function myFunction( test )
            {
            alert( test );
            }
        </script>
        <title>Success</title>
    </head>
    <body>
        <c:set var="test" scope="request" value="${requestScope.userDetails }"></c:set>
        <input type="button" id="sample_button" onclick="myFunction(${test.userName})" value="test" />
    </body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

0

In addition to that, You might also need to add a single quote inside the calling function

<input type="button" id="sample_button" onclick="myFunction('${test.userName}')" value="test">

Check for javascript errors & verify the generated HTML in browser

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.