145

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!

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
X10nD
  • 21,638
  • 45
  • 111
  • 152
  • 4
    The revisions done to this question are awful. It completely removes the reason from vision why the asker was originally having issues. And makes some of the questions point out things that aren't there anymore. – Nol Feb 11 '17 at 01:07
  • I've rolled the question back to its original presentation; the revisions/edits done by other users effectively corrected the entire list of problems the question was asking about, meaning the question no longer made sense ("But it's no longer working" is false) and the accepted answer didn't make sense (it referred to parts of the question that didn't exist). To anyone who edited the question to "improve" the code; please read https://meta.stackoverflow.com/questions/260245/when-should-i-make-edits-to-code TLDR? Only edit question code to format it, never to "fix" it. Post an answer instead – Caius Jard Oct 10 '17 at 16:55

7 Answers7

268

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
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • I am trying to trigger this alert with an append, and the hidden field is in the append div. – X10nD Jun 22 '10 at 08:50
  • @Jean: I don't undertand that, please be more specific. – Sarfraz Jun 22 '10 at 08:55
  • Please check for the updated question http://stackoverflow.com/questions/3091670/get-value-from-hidden-field-after-append-jquery – X10nD Jun 22 '10 at 09:03
13

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();
MERT DOĞAN
  • 2,864
  • 26
  • 28
12

This should work:

var hv = $('#h_v').val();
alert(hv);
dzida
  • 8,854
  • 2
  • 36
  • 57
6

html

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

js

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

example

5
var hiddenFieldID = "input[id$=" + hiddenField + "]";
var requiredVal= $(hiddenFieldID).val();
Zeeshan Ali
  • 343
  • 5
  • 6
4
var x = $('#h_v').val();
alert(x);
Kemal Fadillah
  • 9,760
  • 3
  • 45
  • 63
Cris
  • 12,124
  • 27
  • 92
  • 159
1

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

Coronier
  • 645
  • 1
  • 6
  • 19