简体   繁体   中英

How to put Javascript variable into an html page

I got this line in html :

<input type="text" name="number" value= "" style="width: 20px;border:#FFFFFF none 0px;" readonly></p>

And I also have a variable in a function in javascript that I would like to put in the value of the html line.

http://jsfiddle.net/6v28hj75/1/

function myFunction() {
input = document.getElementsByTagName('input')[0];
input.value = "Your value here"; 
}
myFunction()

I made you a simple example, pure Javascript.

For this purpose you can use jQuery like this :

 jQuery(function($){ var foo = 'bar'; $('input[name="number"]').val(foo); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" name="number" value=""> 

You can access JavaScript variables in JSP as below:

JavaScript:

 var fruit = "Apple";
 $("#someTextBox").val(fruit);

JSP:

<input type="text" id ="someTextBox" name="number" style="width: 20px;border:#FFFFFF none 0px;"></p>

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