Issue
Here is my JSP Code
<%@page import="PresentationPkg.TestCls"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="ex.jsp">
<input type="text" id="resultCode" name="resultCode">
<input type="button" name="viewValue" value="Submit View"/>
<%
String newTestResultCode = TestCls.getMaxTestResultID();
%>
</form>
</body>
</html>
I want to show variable 'newTestResultCode' value in 'resultCode' textBox by clicking button 'Submit View'
Solution
Try Below Code. Do not forget to add jquery js
< script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></ script>
Copy paste below code and run it.
<%@page import="PresentationPkg.TestCls"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String newTestResultCode = TestCls.getMaxTestResultID();
%>
<form action="ex.jsp">
<input type="text" id="resultCode" name="resultCode">
<input id="myButton" type="button" name="viewValue" value="Submit View" />
</form>
<script type="text/javascript">
$(document).ready(function() {
$('#myButton').click(function (){
$("#resultCode").val("<%=newTestResultCode%>");
});
});
</script>
</body>
</html>
You can also download jquery js in your project and use it. The js function will set your variable data into input box.
Answered By - prem30488
Answer Checked By - Marilyn (JavaFixing Volunteer)