Set value of hidden input with jQuery

JavascriptJquery

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

You should use val instead of value.

<script type="text/javascript" language="javascript">
$(document).ready(function () { 
    $('input[name="testing"]').val('Work!');
});
</script>

Solution 2 - Javascript

This worked for me:

$('input[name="sort_order"]').attr('value','XXX');

Solution 3 - Javascript

$('input[name="testing"]').val(theValue);

Solution 4 - Javascript

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>

Solution 5 - Javascript

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

Solution 6 - Javascript

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.

Solution 7 - Javascript

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

Solution 8 - Javascript

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 .

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTonyMView Question on Stackoverflow
Solution 1 - Javascriptuser557419View Answer on Stackoverflow
Solution 2 - JavascriptgzveriView Answer on Stackoverflow
Solution 3 - JavascriptHaim EvgiView Answer on Stackoverflow
Solution 4 - JavascriptarteagasoftView Answer on Stackoverflow
Solution 5 - JavascriptDat NguyenView Answer on Stackoverflow
Solution 6 - JavascriptMaykonnView Answer on Stackoverflow
Solution 7 - JavascriptnzrytmnView Answer on Stackoverflow
Solution 8 - Javascriptrahul krishnaView Answer on Stackoverflow