Madan Madan
Madan Madan

Reputation: 684

Getting Value from html form with the same name attribute under Loop

I have a form which displays multiple rows from database with 4 columns. From these record I need to write a new value in 4th column and update database record. But whenever I try, only First Row value can be updated/read. But not the other rows!! This can be due to the same "name=redirection" as it is given to each from "for loop". So, how can I get the values from other rows too??

for (int i=0; i<domains.size(); i++) {
domainprops = (String[]) domains.get(i);   
%>

<table cellspacing="0" cellpadding="10" border="0" class="tableview" width="100%">
<td width="150"><input type="text"  id="domains" name="domains" value="<%=domainprops[0]%>"></td>
<td width="160"><input type="text"  name="defaulturl" value="<%=domainprops[1]%>" size="30"></td>
<td width="160"><input type="text"  name="redirecturl" value="<%=domainprops[2]%>" size="30"></td>

<td width="160"> <input type="text" id="redirection" name="redirection"></td>

<td align="right"><a href="javascript:win2('recordUpdate.jsp?domains=<%=domainprops[0]%>
')">[Update]</a></td>

    </tr>
 </table>

<% } %>

Javascript Code :

function win2(urlPath) {
    var winl = (screen.width-200)/2;
    var wint = (screen.height-100)/2;
    var settings = 'height=100,width=200,directories=no,resizable=no,status=no,scrollbars=no,menubar=no,location=no,top=' + wint + ',left=' + winl;

    var changeurls=document.getElementById("redirection").value;
    urlPath+='&rdirect='+changeurls
    editWin.focus();
}

Upvotes: 1

Views: 2319

Answers (2)

Elianora Rose
Elianora Rose

Reputation: 194

EDIT: Please read the answer referring to having multiple elements with the same ID. You should not be using multiple of the same ID.

You could use Javascript to iterate over redirection form elements.

function loopThroughRedirection(form) {
    var result = "";

    for (var i = 0; i < form.elements.length; i++) {
        if (form.elements[i].name == 'redirection') {
            // Do something to retrieve the value of redirection
            result += form.elements[i].value
        }
    }

    return result;
}

Upvotes: 2

user2062653
user2062653

Reputation: 31

An ID in the DOM is supposed to be unique. If any element in the DOM has an ID, it should not be shared by any other element.

What I would suggest doing is appending your loop counter on to the end of the ID. This will ensure that every element you create in the DOM has its own unique ID.

for (int i=0; i<domains.size(); i++) {
domainprops = (String[]) domains.get(i);   

...
<input type="text" id="domains_<%= i %>" name="domains" value="<%=domainprops[0]%>">
...
<input type="text" id="redirection_<%= i %>" name="redirection"></td>



</tr>
</table>

}

Next, pass the loop counter to the win2 function call:

<td align="right"><a href="javascript:win2('recordUpdate.jsp?domains=<%=domainprops[0]%>
', <%= i %>)">[Update]</a></td>

Finally, adjust the function itself...

function win2(urlPath, loopID) {
...
    var changeurls=document.getElementById("redirection_" + loopID).value;
    urlPath+='&rdirect='+changeurls
...
}

Upvotes: 3

Related Questions