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?
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?
Share Improve this question asked Aug 21, 2013 at 7:18 newbienewbie 5372 gold badges8 silver badges13 bronze badges2 Answers
Reset to default 1You 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>
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