1

I have created a form in a modal box with a table. When you click on a table row it populates a form on the parent page. This is working great but I can't get the values to change when I click on another table row. Because I'm using IDs it only takes the values from the first table row. I am open to changing to classes but I tried this and it didn't help.

Please can anyone help me with this. The code is below.

Modal Form with Table

<div id="modal_form" title="Address Search">
<form id="modal_form">
<ul>
    <li><label for="name">Search by street description</label>
        <input type="text" name="street_description" id="street_description" />
        <input type="button" id="search_button" class="form_button" value="Search"></li>
</ul>
<table width="100%" border="0" cellspacing="0" cellpadding="0" id="table-data">
              <tbody><tr>
                <td width="200px"><a href="#">Street</a></td>
                <td width="200px"><a href="#">Suburb</a></td>
                <td width="200px"><a href="#">City</a></td>
              </tr>
              <tr>
                <td id="address_street">Harambee Road</td>
                <td id="address_suburb">Onerai </td>
                <td id="address_city">Onerai Rural</td>
              </tr>
              <tr>
                <td id="address_street">Hutchinson Road</td>
                <td id="address_suburb">Onerai </td>
                <td id="address_city">Onerai Rural</td>
              </tr>
              <tr>
                <td id="address_street">Kauri Road</td>
                <td id="address_suburb">Onerai </td>
                <td id="address_city">Onerai Rural</td>
              </tr>
        </tbody></table><!-- /table#table-data -->
</form>

Javascript (This is where I need to differentiate between the table rows)

<script type="text/javascript">
$(document).ready(function() {
$('#table-data tr').click(function () {
$('#street_name').val( $('#address_street').text() )
$('#suburb').val( $('#address_suburb').text() )
$('#city').val( $('#address_city').text() )})
});
</script>

Parent Form, where the form fields are populated

<form id="profile">
<ul>
<li><label for="street_number">Street Number</label><input id="street_number" type="text" placeholder="Street Number" name="street_number" ><input type="button" class="form_button" id="find_address" value="Find Address"></li>
<li><label for="street_name">Street Name</label><input id="street_name" type="text" placeholder="Street Name" name="street_name"  disabled="disabled" ></li>
<li><label for="suburb">Suburb</label><input id="suburb" type="text" placeholder="Suburb" name="suburb"  disabled="disabled" ></li>
<li><label for="city">City</label><input id="city" type="text" placeholder="City" name="city" disabled="disabled" ></li>
<li><input type="submit" id="submit" value="Save"></li>
</ul>
</form>

Thanks for any help you can give, cheers

1 Answer 1

2

IDs must be unique, so this is not going to work:

<td id="address_street">Harambee Road</td>
...
<td id="address_street">Hutchinson Road</td>

I would give each of the rows an ID, and have all of the columns use the same class:

<tr id="row1">
  <td class="address_street">Harambee Road</td>
  <td class="address_suburb">Onerai </td>
  <td class="address_city">Onerai Rural</td>
</tr>
<tr id="row2">
  <td class="address_street">Hutchinson Road</td>
  <td class="address_suburb">Onerai </td>
  <td class="address_city">Onerai Rural</td>
</tr>
<tr id="row3">
  <td class="address_street">Kauri Road</td>
  <td class="address_suburb">Onerai </td>
  <td class="address_city">Onerai Rural</td>
</tr>

Your JavaScript would change to be like this:

<script type="text/javascript">
  $(document).ready(function() {
    $('#table-data tr').click(function () {
      var curRowId = $(this).attr("id");
      $('#street_name').val( $('#' + curRowId + ' td.address_street').text() );
      $('#suburb').val( $('#' + curRowId + ' td.address_suburb').text() );
      $('#city').val( $('#' + curRowId + ' td.address_city').text() );
    });
  });
</script>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, any idea how I could modify my javascript to incorporate your solution. I'm still a beginner and to use your solution I would create multiple scripts just targeting each row ID. I know there must be an easier way to do this, any suggestions? Thanks
Sorry disregard my above comment, I obviously commented while you were busy, I'll try your code out now, thanks again :)
@Clinton: Sorry, I missed some spaces in the JavaScript; updated.
GENIUS - It works thanks so much for all the help and effort, I was really struggling with this one, I'll try use var more in the future. Cheers :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.