Juliette Dupuis
Juliette Dupuis

Reputation: 1037

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.

Upvotes: 0

Views: 7003

Answers (1)

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79830

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

Upvotes: 8

Related Questions