Reputation: 573
I want to get the value inside a textfield. The textField is inside a <td>
in a table. When I click the button I want the text which is inside a textfield, with the lables it works perfect.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Bind Click Event to Dynamically added Elements</title>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#main').on("click", 'input[type="button"]', function () {
var customerId = $(this).parent().siblings('td:eq(2)').text();
$('#texti').text(customerId);
});
});
</script>
</head>
<body>
<button>Add new list item</button>
<p>Click the above button to dynamically add new list items. You can remove it later.</p>
<table id="main">
<tr>
<td>list item 1 </td>
<td><input type='text' name='textN' id='textN' value='asdf'/></td>
<td>list item 3 </td>
<td ><input type='button' value='Update' name ='updateBox' id='updateBox' /></td>
</tr>
<tr>
<td>list item 4</td>
<td><input type='text' name='textN' id='textN' value='asdf2'/></td>
<td>list item 6</td>
<td ><input type='button' value='Update' name ='updateBox' id='updateBox' /></td>
</tr>
<tr>
<td>list item 7</td>
<td><input type='text' name='textN' id='textN' value='asdf3'/></td>
<td>list item 9</td>
<td ><input type='button' value='Update' name ='updateBox' id='updateBox' /></td>
</tr>
</table>
<a name="texti" id="texti"> x </a>
</body>
</html>
It gives me a blank variable. how to fix?
Upvotes: 2
Views: 260
Reputation: 48437
Only thing you need is to find
input from current row and get it's value using .val()
.
Here is solution:jsfiddle
$('#main').on("click", 'input[type="button"]', function () {
var customerId = $(this).parent().siblings('td:eq(1)').find('input').val();
$('#texti').text(customerId);
});
Upvotes: 2
Reputation: 2378
Use .val()
instead of .text()
. .val()
is used for input values while .text()
tries to get text between tags.
Upvotes: 0