user327712
user327712

Reputation: 3321

Is it possible to add a value to a hidden text box field in JQuery

Recently, I came across a web page where someone asked how to add some value to a hidden field.
Since I am new to JQuery, I tried using Javascript instead and came up with this code:

<html>
<head>
<script type="text/javascript">
<!--
function t() {
document.testform.testfield.value = "helloworld";
}
-->
</script>
</HEAD>
<body onLoad="t()">
<form name="testform" id="testform">
<input type="hidden" name="testfield" id="testfield">
</form>
</body>
</html> 

The code works only in non-hidden Text Box field.
I am not sure if JQuery can do it.
Is it possible to add a value to a hidden text box field in JQuery?

Please help if you could.

Upvotes: 1

Views: 2733

Answers (1)

cusspvz
cusspvz

Reputation: 5281

yes ;)

$("input[name=testfield]").attr("value","helloworld");

or

$("#testfield").attr("value","helloworld");

or

$("#testfield").val("helloworld"); //Thx Felix Klinger

and if you wan't to show the form:

$("input[name=testfield]").attr("type","text");

or

$("#testfield").attr("type","text");

Upvotes: 4

Related Questions