views:

69

answers:

4

Hi there.

I have a problem setting value of an hidden input element. I've tried using jQuery and $("#SomeHiddenElement").val(sSomeValue) function, and plain JS document.getElementById("SomeHiddenElement").value = sSomeValue; but nothing works...

When I set the element to text type it works just fine...

The problem persists both in FF and IE.

Any ideas?

Code:

<input type="hidden" id="SomeHiddenElement" name="SomeHiddenElement" value="" />

document.getElementById("SomeHiddenElement").value = "Testing";
+1  A: 

Your code is fine.

<!DOCTYPE html>
<html>
<head> 
   <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
   <title>Setting the value of a hidden field</title> 
</head> 
<body> 
  <input type="hidden" id="SomeHiddenElement" value="">
  <script>
    document.getElementById('SomeHiddenElement').value = 'Hello World';
  </script>

  <script>
    // Will alert 'Hello World'
    alert(document.getElementById('SomeHiddenElement').value);
  </script>
</body> 
</html>

How are you testing that the value of the hidden field is not being set? Since hidden fields are invisible, you can see what's inside when with JavaScript, or when you post the form.

Daniel Vassallo
I'm monitoring the value with Firebug.
Stazh
+1  A: 

Can you try:

$('#SomeHiddenElement').attr("value", "some text");

this is just a simple jQuery solution to set the attribute "value" with test text.

Edit:

Well, try this:

alert($("input[type='hidden']").length);

This is to see how many hidden input elements on your page.

Michael Mao
It does not work.
Stazh
I'm not new to HTML/JS/jQeury. I know all the methods, but the don't work in this case and I don't know why... I thought that someone else had the same problem to post the solutio here...
Stazh
+2  A: 

Since I don't believe firebug has a problem updating it's DOM representation, and your code works fine in isolation, and you don't have an issue with text inputs, and I've never had a problem updating hidden inputs myself, I would suggest that something else is acting on hidden inputs to block what you're doing.

I suggest you create a test page stripped of all content except what you've given us here and then incrementally add features to progress towards the state of your real page. At some point it will break and you'll at least know where the problem lies.

annakata
@Stazh: if this worked for you could you illuminate us and future readers what the problem turned out to be?
annakata
A: 

Guys I'm so sorry... The problem was in combination of Firebug and my post handling code... I've fixed it...

Moral of the story: double check code and don't blindly believe Firebug.

Thanks for your trouble!

Stazh