RAJESHKUMAR P R RAJESHKUMAR P R - 2 months ago 10
jQuery Question

How do i fetch dynamically created text box values in jsp page?

I have doubt regarding dynamically created textbox values in JSP file. My files contains HTML and Javascript (home.jsp) and JSP (abc.jsp).

In this example I can fetch values only initially created textboxes, I can't fetch values from dynamically created textboxes. How do I fetch values from each dynamically created textboxes at run time in the JSP file? How do I fetch total number of textboxes created at run time?

home.jsp


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
var addDiv = $('#addinput');
var i = $('#addinput p').size() + 1;

$('#addNew').live('click', function() {
$('<p><input type="text" id="p_new" size="20" name="p_new_' + i +'" value="" placeholder="I am New" /><input type="text" id="p_new1" size="20" name="p_new1_' + i +'" value="" placeholder="I am New" /><a href="#" id="remNew">Remove</a> </p>').appendTo(addDiv);
i++;
document.getElementById('raj').value = i;
return false;
});

$('#remNew').live('click', function() {
if (i > 2) {
$(this).parents('p').remove();
i--;
}
return false;
});
});
</script>
</head>
<body>
<form action="abc.jsp" method="post">
<h2>Dynammically Add Another Input Box</h2>
<div id="addinput">
<p>
<input type="text" id="p_new" size="20" name="p_new" value="" placeholder="Input Value" /><input type="text" id="p_new1" size="20" name="p_new1" value="" placeholder="Input Value" /><a href="#" id="addNew">Add</a>
</p>
</div>
<input type="submit" value="SUBMIT">
</form>
</body>
</html>`


abc.jsp

<%
String name = request.getParameter("p_new");
String name1 = request.getParameter("p_new1");
String name2 = request.getParameter("p_new_[0]");
System.out.println(name);
System.out.println(name1);
System.out.println(name2);
%>

Answer

provide same names to input fields

then use getParameterValues('p_new') it will return String array

Update

if your are sending multiple input then first, second i.e ur static input too rename with p_new

then you are able to access using getParameterValues('p_new')

your static name not match with dynamic created input

Comments