Reputation: 2057
I was wondering if there is an easy way to convert an html table that has textboxes as inputs into new table with the textbox values.
For example:
I have this table in which a user can insert data.
<table>
<tr>
<td>
<input id="name" type="text"/>
</td>
<td>
<input id="phone" type="text"/>
</td>
<td>
<input id="email" type="text"/>
</td>
</tr>
</table>
The user then inserts "john" into the name box, "(987)891-9819" into the phone box, and "[email protected]" into the email box.
I want my result table to look like this:
<table>
<tr>
<td>
john
</td>
<td>
(987)891-8919
</td>
<td>
[email protected]
</td>
</tr>
</table>
I am looking for a way to accomplish this in javascript / jquery.
Upvotes: 0
Views: 101
Reputation: 318302
It's actually as easy as
$('input').replaceWith(function() {
return this.value;
});
Upvotes: 2
Reputation: 104785
You can do:
function convertTable() {
$("table tr td :input:text").each(function() {
var value = this.value;
$(this).parent("td").empty().text(value);
});
}
Upvotes: 1