114

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
  • 39,551
  • 56
  • 175
  • 291
TonyM
  • 1,141
  • 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

144

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,570
  • 12
  • 59
  • 95
37

This worked for me:

$('input[name="sort_order"]').attr('value','XXX');
Colin Brock
  • 21,267
  • 9
  • 46
  • 61
gzveri
  • 371
  • 3
  • 2
32
$('input[name="testing"]').val(theValue);
Kevin van Mierlo
  • 9,554
  • 5
  • 44
  • 76
Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
  • 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>
Ashish Neupane
  • 193
  • 2
  • 17
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
8
var test = $('input[name="testing"]:hidden');
test.val('work!');
Dat Nguyen
  • 1,881
  • 17
  • 36
5

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,738
  • 6
  • 33
  • 51
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
  • 6,193
  • 1
  • 41
  • 38
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 .