0

this is javascript code where I want to change the name attribute each time new element is created.I am creating new textbox each time when the new table row is added. but when I try to access values of these elements from servlet it shows me null value.What exactly I am doing wrong? any help would be appreciated. Thanx in advance!!

<html>
<head>
<script>

function addRow()
 {
    var table = document.getElementById('table');
    var rowCount = table.rows.length;
    var row = table.insertRow(rowCount);
    var cell1 = row.insertCell(0);
    var element1 = document.createElement("input");
    element1.type = "text";
    element1.name ="item"+rowCount;
    cell1.appendChild(element1);     
    var cell2 = row.insertCell(1);
    var element2 = document.createElement("input");
    element2.type = "text";
    element2.name ="amount"+rowCount;   
    cell2.appendChild(element2);
    var cell3 = row.insertCell(2);
    cell3.innerHTML = ' <input type="button" value="Edit" onclick="editRow(this)"/>           <input type="button" value="Delete" onclick="deleteRow(this)"/>';
    cell3.setAttribute("style", "display:none;");
    var cell4 = row.insertCell(3);
    cell4.innerHTML = '<input type="button" value="Save" onClick="saveRow(this)">';

    document.listform.hfield.value=rowCount;
}
function submitList()
 {
 document.listform.submit();    
 }
</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Create your list</title>
</head>
<body>
<form name="listform" method="post" action="Billingservlet">
<div class="wrap">
<center>
<div id="display">
<table id='table' border="0">
<tr id='id'>
<th>Item Name</th>
<th>No. of units</th>   
</tr>   
</table>    
<input type="button" value="Add another item" onclick="addRow()">
<input type="button" value="Submit List" onclick="submitList()">
<input type="hidden" name="hfield">

my Servlet code is

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out= response.getWriter();
int i=1;
String val1=request.getParameter("item2");
out.print(val1);

I checked what data is going to servlet using chrome:

hfield:2
Response Headersview parsed
HTTP/1.1 200 OK
server: Apache-Coyote/1.1
Content-Length: 4
Date: Fri, 13 Sep 2013 02:21:29 GMT

2 Answers2

0

1) you can use element.name=newName; or
2) you can use element.setAttribute("name",newName);

Either of these will set the "name" attribute of your element.

so...

var element1 = document.createElement("input");
element1.name="myInput";

...or...

var element1 = document.createElement("input");
element1.setAttribute("name","myInput");
Duncan
  • 1,530
  • 15
  • 20
  • I tried using both ways but still I am getting null value when I tried to print it using request.getParameter("paramvalue") – user2773665 Sep 12 '13 at 17:55
  • Where is request coming from? Also, where is .getParameter() defined? – Duncan Sep 12 '13 at 18:08
  • I am trying to print the value in servlet using getParameter method – user2773665 Sep 12 '13 at 18:16
  • 1
    I might suggest you do a bit more research, then edit your original post to better reflect what you're trying to accomplish. Your current question (how to change the name of an html element) has been answered. I guessing you're referencing a Java Applet (servlet) and attempting to get an HTML element via the applet? (keep in mind that Java is NOT JavaScript by any means) – Duncan Sep 12 '13 at 18:24
0

I think you are missing quite a bit of relevant code here. At what point does anything get posted to the servlet? Where is this code? Are you making an AJAX call to your servlet? Where is the input defined that has the name field of "item1" or "item2"? Are you sending an array of items with an AJAX post? Exactly what is going on?

Your provided JavaScript function has no call to the server, I'm not even sure how you're seeing a null value being printed out.

If you want to see all the parameters being sent to the the servlet, why not do a loop to print them out?

    for (String name : request.getParameterMap().keySet()) {
        System.out.println("Name: " + name + " = " + request.getParameter(name));
    }

Post your other JavaScript functions or related HTML forms where the data is actually being sent to the server and it might be easier to determine the problem.

Russell Shingleton
  • 3,176
  • 1
  • 21
  • 29