Lamloumi Afif
Lamloumi Afif

Reputation: 9081

Getting input hidden value with JQuery

I have a javascript snippet in which I'd like to retrieve the value of a hidden input located in the first column of selected row :

var global_id = $(this).find("td:first").html();
console.log("value=" + global_id);

I get as result :

value =<input id="id" name="id" type="hidden" value="2">

When I try

 var global_id = $(this).find("td:first").val();
console.log("value=" + global_id);

I get as result :

value =

So, I need to know :

  1. Why in the second way, the variable is empty
  2. How can I resolve my code to retrieve the first input hidden value?

Upvotes: 0

Views: 572

Answers (1)

user4639281
user4639281

Reputation:

You need to reference the actual input element, not the containing element. This will find the input element if it is hidden using type="hidden", using css or using jQuery's hide()

var global_id = $("td:first input:hidden:first", this).val();
console.log("value=" + global_id);

Upvotes: 6

Related Questions