TonyM
TonyM

Reputation: 1181

Set value of hidden input with jQuery

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?

Upvotes: 118

Views: 271096

Answers (8)

rahul krishna
rahul krishna

Reputation: 1

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 .

Upvotes: -1

arteagasoft
arteagasoft

Reputation: 151

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>

Upvotes: 12

nzrytmn
nzrytmn

Reputation: 6941

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

Upvotes: 2

Haim Evgi
Haim Evgi

Reputation: 125496

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

Upvotes: 32

user557419
user557419

Reputation:

You should use val instead of value.

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

Upvotes: 149

Maykonn
Maykonn

Reputation: 2928

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.

Upvotes: 5

Dat Nguyen
Dat Nguyen

Reputation: 1891

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

Upvotes: 8

gzveri
gzveri

Reputation: 371

This worked for me:

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

Upvotes: 37

Related Questions