Get value from hidden field using jQuery

Jquery

Jquery Problem Overview


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!

Jquery Solutions


Solution 1 - Jquery

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

Solution 2 - Jquery

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();

Solution 3 - Jquery

This should work:

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

Solution 4 - Jquery

html

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

js

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

example

Solution 5 - Jquery

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

Solution 6 - Jquery

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

Solution 7 - Jquery

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionX10nDView Question on Stackoverflow
Solution 1 - JquerySarfrazView Answer on Stackoverflow
Solution 2 - JqueryMERT DOĞANView Answer on Stackoverflow
Solution 3 - JquerydzidaView Answer on Stackoverflow
Solution 4 - Jqueryuser2985029View Answer on Stackoverflow
Solution 5 - JqueryZeeshan AliView Answer on Stackoverflow
Solution 6 - JqueryCrisView Answer on Stackoverflow
Solution 7 - JqueryCoronierView Answer on Stackoverflow