I have a table with multiple rows and one column. Each table cell has a button in it. Like this:
<table id="table1" border="1">
<thead>
<tr>
<th>Select</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<form name="f2" action="javascript:select();" >
<input id="edit" type="submit" name="edit" value="Edit" />
</form>
</td>
</tr>
</tbody>
</table>
What I want to do: when one of the buttons is pressed I would like to change its value from "Edit" to "Modify". Any idea?
<tr>: The Table Row element. The <tr> HTML element defines a row of cells in a table. The row's cells can then be established using a mix of <td> (data cell) and <th> (header cell) elements.
Pretty sure this solves what you're looking for:
HTML:
<table>
<tr><td><button class="editbtn">edit</button></td></tr>
<tr><td><button class="editbtn">edit</button></td></tr>
<tr><td><button class="editbtn">edit</button></td></tr>
<tr><td><button class="editbtn">edit</button></td></tr>
</table>
Javascript (using jQuery):
$(document).ready(function(){
$('.editbtn').click(function(){
$(this).html($(this).html() == 'edit' ? 'modify' : 'edit');
});
});
Edit:
Apparently I should have looked at your sample code first ;)
You need to change (at least) the ID attribute of each element. The ID is the unique identifier for each element on the page, meaning that if you have multiple items with the same ID, you'll get conflicts.
By using classes, you can apply the same logic to multiple elements without any conflicts.
JSFiddle sample
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With