最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can't set value on hidden input element - Stack Overflow

programmeradmin6浏览0评论

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";

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";
Share Improve this question asked Oct 6, 2010 at 7:24 StazhStazh 2071 gold badge4 silver badges13 bronze badges 2
  • here's a simple one, did you put that inside the ready handler? – Reigel Gallarde Commented Oct 6, 2010 at 8:03
  • 2 @Reigel - he said it works if it bees a text input, so one would assume yes. – annakata Commented Oct 6, 2010 at 8:29
Add a ment  | 

6 Answers 6

Reset to default 7

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.

Guys I'm so sorry... The problem was in bination 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!

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.

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.

These both work:

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

Verified using Chrome Developer Tools and Firebug

I know it's been a long time, but I had the same issue, and it took me some time to find out that I had more than one element ( to be exact) on page with the same id="" and it seemed that my JS function did not work (which I guess it was modifying value of just the first element). Anyway, this may help someone save some time investigating :)

发布评论

评论列表(0)

  1. 暂无评论