简体   繁体   中英

Pass a javascript variable to html. jQuery

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM