Set value of hidden field in a form using jQuery's ".val()" doesn't work !

Posted by texens on Stack Overflow See other posts from Stack Overflow or by texens
Published on 2010-06-05T09:08:51Z Indexed on 2010/06/05 9:12 UTC
Read the original article Hit count: 324

Filed under:
|
|
|

I've been trying to set the value of a hidden field in a form using jQuery, but without success.

Here is a sample code that explains the problem. If I keep the input type to "text", it works without any trouble. But, changing the input type to "hidden", doesn't work !

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
    $("input:text#texens").val("tinkumaster");
  });
});
</script>
</head>
<body>
<p>Name: <input type="hidden" id="texens" name="user" value="texens" /></p>
<button>Change value for the text field</button>
</body>
</html>

I also tried the following workaround, by setting the input type to "text" and then using a "display:none" style for the input box. But, this also fails ! It seems jQuery has some trouble setting hidden or invisible input fields.

Any ideas? Is there a workaround for this that actually works?

Thanking in anticipation

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about value