admin管理员组

文章数量:1316688

I have a variable in Javascript:

var test ="whatever"

I just want to pass this variable inside a hidden input:

<input type="hidden" class="myinput" value="">

I tried:

$('.myinput').attr('test');

But it does not seem to work.

Thank you for your help.

I have a variable in Javascript:

var test ="whatever"

I just want to pass this variable inside a hidden input:

<input type="hidden" class="myinput" value="">

I tried:

$('.myinput').attr('test');

But it does not seem to work.

Thank you for your help.

Share Improve this question edited Nov 12, 2012 at 22:48 Nat Ritmeyer 5,6788 gold badges48 silver badges59 bronze badges asked Nov 12, 2012 at 22:43 Juliette DupuisJuliette Dupuis 1,0372 gold badges14 silver badges22 bronze badges 1
  • Did it help, if so please mark it is as accepted for closure. – Selvakumar Arumugam Commented Nov 13, 2012 at 20:00
Add a ment  | 

1 Answer 1

Reset to default 8
  1. Use .val function to set the value of hidden input.
  2. Remove the quotes to consider it as a variable and not a string literal.

See below code:

$('.myinput').val(test);

本文标签: Pass a javascript variable to html jQueryStack Overflow