Unknown Soldier
Unknown Soldier

Reputation: 51

jQuery get value from hidden input in column

I have got the following HTML table...

<table>
  <thead>
    <tr>
      <th>Nr.</th>
      <th>Name</th>
      <th>Info</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>Laura</td>
      <td><input type="hidden" value="1"><a href="#" class="info">Info</a></td>
    </tr>
    <tr>
      <td>2</td>
      <td>Sabrina</td>
      <td><input type="hidden" value="2"><a href="#" class="info">Info</a></td>
    </tr>
  </tbody>
</table>

How can I get with jQuery the value of the hidden input field, when the link is clicked?

$(".info").click(function() {
  // Here I need to find out the value...
});

Upvotes: 0

Views: 8123

Answers (2)

krishwader
krishwader

Reputation: 11371

Here's how you do it :

$(".info").click(function(e) {
  //just in case, will be useful if your href is anything other than #
  e.preventDefault();
  alert($(this).prev('input[type="hidden"]').val());
});

prev method will search for the previous element, which is where your input[hidden] is.

And its href, not hre, in the <a/> tags.

Upvotes: 3

David
David

Reputation: 1889

You can also use attributes <a href="#" data-hidden="1" class="info"> don't need use hidden fields

$(".info").click(function(e) {
  e.preventDefault();
  alert($(this).data('hidden')); // or $(this).attr('data-hidden');
});

Upvotes: 2

Related Questions