Reputation: 9977
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
Upvotes: 1
Views: 5465
Reputation: 5945
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>
Upvotes: 2