X10nD
X10nD

Reputation: 22050

Get value from hidden field using jQuery

I have a <input type="hidden" value="" id='h_v' class='h_v'> Using jQuery I want to alert the user to this value .

I am using

var hv = $('#h_v).text();
alert('x');

But its not working, any clues!

Upvotes: 146

Views: 505071

Answers (7)

MERT DOĞAN
MERT DOĞAN

Reputation: 3116

If you don't want to assign identifier to the hidden field; you can use name or class with selector like:

$('input[name=hiddenfieldname]').val();

or with assigned class:

$('input.hiddenfieldclass').val();

Upvotes: 14

user2985029
user2985029

Reputation:

html

<input type="hidden" value="hidden value" id='h_v' class='h_v'>

js

var hv = $('#h_v').attr("value");
alert(hv);

example

Upvotes: 6

Zeeshan Ali
Zeeshan Ali

Reputation: 341

var hiddenFieldID = "input[id$=" + hiddenField + "]";
var requiredVal= $(hiddenFieldID).val();

Upvotes: 5

Cris
Cris

Reputation: 12204

var x = $('#h_v').val();
alert(x);

Upvotes: 4

dzida
dzida

Reputation: 9001

This should work:

var hv = $('#h_v').val();
alert(hv);

Upvotes: 12

Sarfraz
Sarfraz

Reputation: 382909

Use val() instead of text()

var hv = $('#h_v').val();
alert(hv);

You had these problems:

  • Single quotes was not closed
  • You were using text() for an input field
  • You were echoing x rather than variable hv

Upvotes: 270

Coronier
Coronier

Reputation: 645

Closing the quotes in var hv = $('#h_v).text(); would help I guess

Upvotes: 1

Related Questions