Quaking-Mess
Quaking-Mess

Reputation: 525

Jquery: on click table row, find value of contained input text?

 <table class="container">
 <tr>
   <td><input type="text" class="foo"></td>
 </tr>
 <tr>
   <td><input type="text" class="foo"></td>
 </tr>
 <tr>
   <td><input type="text" class="foo"></td>
 </tr>
 </table>


 <script>
 $(".container tr").click(function(){
     alert($(this).find("foo").val());
 });
 </script>

What it's supposed to do:
When I click on a table row it will find the input inside this element and alert it's value.

Thank You!

Upvotes: 0

Views: 19177

Answers (4)

Ajay Thakkar
Ajay Thakkar

Reputation: 46

please make the following changes you will 100% get the solution.

Change the jquery as below,

function getvalue(elementid) {

var elementid = "#" + elementid;
alert($(elementid).val());
}

and change your HTML as below,`enter code here`

as shown here you need  to call jquery function on click event of each textbox and     also need to give an id to each text box.
<table class="container">
<tr>
<td>
<input type="text" id="txtval1" class="foo" onclick="javascript:test(this.id);"></td>
</tr>
<tr>
<td>
<input type="text" id="txtval2" class="foo" onclick="javascript:test(this.id);"></td>
</tr>
<tr>
<td>
<input type="text" id="txtval3" class="foo" onclick="javascript:test(this.id);"></td>
</tr>
</table>

Upvotes: 0

codingbiz
codingbiz

Reputation: 26376

$(this).find(".foo") would return multiple items i.e. there are 3 element with that class. Note the addition of . to indicate it's a class name

$(".container tr").click(function(){
     $(this).find(".foo").each(function(){
          alert($(this).val());

      });
 });

$(this).find("foo") would try to find an element with 'foo' tag e.g. <foo></foo>

Upvotes: 1

Eli
Eli

Reputation: 14827

You miss the . inside your class selector:

alert($(this).find(".foo").val()); 

FIDDLE: http://jsfiddle.net/wUgt5/

Upvotes: 1

Billy Moon
Billy Moon

Reputation: 58521

should be .find(".foo") to select class rather than tag

Upvotes: 0

Related Questions