107

I'm trying to set the value of the hidden field below using jQuery.

<input type="hidden" value="" name="testing" />

I'm using this code:

var test = $("input[name=testing]:hidden");
test.value = 'work!';

But its not working. What's wrong with my code?

Uwe Keim
  • 38,279
  • 56
  • 171
  • 280
TonyM
  • 1,071
  • 2
  • 7
  • 3
  • 5
    For full clarity, jQuery returns a collection of DOM elements. test doesn't become the DOM element it becomes an array. To use plain ol' javascript a la .value, you can do test[0].value or loop over the collection if there is more than one. Otherwise, test.val('work!') would work for you since it is a jQuery method rather than test.value which is a property of the DOM element object. – Scottux Oct 04 '12 at 22:48

8 Answers8

139

You should use val instead of value.

<script type="text/javascript" language="javascript">
$(document).ready(function () { 
    $('input[name="testing"]').val('Work!');
});
</script>
Sinister Beard
  • 3,535
  • 11
  • 55
  • 92
34

This worked for me:

$('input[name="sort_order"]').attr('value','XXX');
Colin Brock
  • 20,857
  • 9
  • 45
  • 61
gzveri
  • 341
  • 3
  • 2
32
$('input[name="testing"]').val(theValue);
Kevin van Mierlo
  • 9,282
  • 5
  • 40
  • 67
Haim Evgi
  • 120,002
  • 45
  • 212
  • 219
  • 1
    strange, its supposed to do it – Haim Evgi Jan 26 '11 at 09:38
  • There isn't a problem because of "Type='hidden'" rather than "type='hidden'" is there? Not sure about mixed case attributes. Could be clutching at straws... – Hogsmill Jan 26 '11 at 09:58
  • 1
    i think the problem here is the selector.. I think it should be `input[name="testing"` --> testing between quotes... Whenever setting some value doesn't work, try to get the element first.. If you don't get it, the problem isn't with .val(), but probably with the selector – Thomas Mulder Jan 30 '16 at 17:05
10

Suppose you have a hidden input with id XXX, if you want to assign a value to the following

<script type="text/javascript">

    $(document).ready(function(){
    $('#XXX').val('any value');
    })
</script>
arteagasoft
  • 131
  • 1
  • 6
  • FYI, Code-only answers generally get flagged as low-quality and will usually be deleted. Try posting some context / explanation with your answer if you'd like it to stick around and (possibly) get upvoted. – sgress454 May 09 '14 at 18:23
  • The selector you are using (`#XXX`) is for the `id` attribute. If you "named" an input XXX, the selector would be `input[name="XXX"]`. – krummens May 23 '17 at 01:35
7
var test = $('input[name="testing"]:hidden');
test.val('work!');
Dat Nguyen
  • 1,841
  • 16
  • 36
4

To make it with jquery, make this way:

var test = $("input[name=testing]:hidden");
test.val('work!');

Or

var test = $("input[name=testing]:hidden").val('work!');

See working in this fiddle.

Maykonn
  • 2,509
  • 6
  • 28
  • 50
0

You don't need to set name , just giving an id is enough.

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

and than with jquery you can use 'val()' method like below:

 $('#testId').val("work");
nzrytmn
  • 4,937
  • 1
  • 37
  • 33
0

you can set value to input hidden filed in this way also

 <input type="hidden" value="" name="testing" id-"testing" />
document.getElementById("testing").value = 'your value';

this is one of the method .