Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign, clear and get value of a hidden field in jquery?

Tags:

html

jquery

Consider a hiddenfield in the page HfId

<input type="hidden" id="HfId"/>

How to assign,clear and get value of a hidden field in jquery? ANy suggestion...

like image 538
ACP Avatar asked Apr 01 '10 10:04

ACP


2 Answers

The Element Selector of jQuery is exactly designed for this. It allows you to select all elements which fit the given name, id or class:

$("a")       // Get all a-elements
$("#id")     // Get all elements with the id "id"
$(".class")  // Get all elements with the class "class"

The visibility or other properties of the element does not interfere with that, as it is still present in the DOM of the document.

Fields have the overloaded function val(), val($value) which allows you to get and set the value of the field:

$val = $("#HfId").val();  // Get
$("#HfId").val("");       // Clear
$("#HfId").val($newVal);  // Set
like image 73
Bobby Avatar answered Oct 19 '22 11:10

Bobby


$("#HfId").val("pony");
var x = $("#HfId").val();
like image 25
Svante Svenson Avatar answered Oct 19 '22 13:10

Svante Svenson