Hi,
Im new to javaScript. Im trying to fetch data from html table row to text box by clicking radio button. But i can fetch only the first row. Here iam attaching my code...

JSP Code

<table>
  <tr>
    <td>Select</td>
    <td>ID</td>
    <td>FIRST NAME</td>
    <td>LAST NAME</td>
    <td>AGE</td>
    <td>HOME TOWN</td>
    <td>JOB</td>

  </tr>
   <% while( rs.next() ){%>
  <tr><td><input type="radio" onClick="swap()"></div></td>
    <td><a id="text1" name="text1" ><%=rs.getInt("id")%></a></td>
    <td><a id="text2" name="text1" ><%=rs.getString("firstname")%></a></td>
    <td><a id="text3" name="text1" ><%=rs.getString("lastname")%></a></td>
    <td><a id="text4" name="text1" ><%=rs.getString("age")%></a></td>
    <td><a id="text5" name="text1" ><%=rs.getString("hometown")%></a></td>
    <td><a id="text6" name="text1" ><%=rs.getString("job")%></a></td>
    </tr>
  <%}%>
</table>

Javascript Function

<script>
 function swap(){

 document.getElementById('id').value=document.getElementById('text1').innerHTML;
 document.getElementById('first_name').value= document.getElementById('text2').innerHTML;
 document.getElementById('last_name').value=document.getElementById('text3').innerHTML;
 document.getElementById('age').value= document.getElementById('text4').innerHTML;
 document.getElementById('home_town').value=document.getElementById('text5').innerHTML;
 document.getElementById('job').value= document.getElementById('text6').innerHTML;
 }
</script>

HTML form

<form id="form1" name="form1" method="post" action="">
  <table width="353" border="1">
    <tr>
      <td width="193">ID</td>
      <td width="144"><label>
        <input name="id" type="text" id="id" />
      </label></td>
    </tr>
    <tr>
      <td>FIRST NAME </td>
      <td><label>
        <input name="first_name" type="text" id="first_name" />
      </label></td>
    </tr>
    <tr>
      <td>LAST NAME </td>
      <td><label>
        <input name="last_name" type="text" id="last_name" />
      </label></td>
    </tr>
    <tr>
      <td>AGE</td>
      <td><label>
        <input name="age" type="text" id="age" />
      </label></td>
    </tr>
    <tr>
      <td>HOME TOWN </td>
      <td><label>
        <input name="home_town" type="text" id="home_town" />
      </label></td>
    </tr>
    <tr>
      <td>JOB</td>
      <td><label>
        <input name="job" type="text" id="job" />
      </label></td>
    </tr>
    <tr>
      <td colspan="2"><label>
          <div align="center">
            <input type="submit" name="Submit" value="Submit" />
          </div>
        </label></td>
    </tr>
  </table>
</form>

I THINK HERE I NEED TO GIVE THE <A> TAG ID DYNAMICALLY. BUT I DONT KNOW HOW TO DO THAT. ANY ONE PLEASE HELP ME.

THANKING YOU,

Edited 3 Years Ago by mike_2000_17: Fixed formatting

JS can't make any sense of the stuff inside the % while tag, because:

- It is dynamically created at runtime, but not by JS.

- You have multiple occurrences of the same id as the while repeats. This is not allowed.

This article has been dead for over six months. Start a new discussion instead.