简体   繁体   中英

How to get return value from javascript to html?

How can I return a value from java script to HTML?

Assume this is my JS file named first.js

function getName()
{
var Str= "Vinod";
return Str;
}

Now I need to get the value in HTML page. I have included the js file and called the function but I didn't get any value. How can I do the same.

<html>
<head>
<title>Quick example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="first.js"></script>
<script type="text/javascript" src="static/jquery.js"></script>
<link rel="stylesheet" href="static/jquery.mobile-1.4.2.css" />
script type="text/javascript" src="static/jquery.mobile-1.4.2.js"></script>
<script type="text/javascript">
$(document).ready(function() {
document.getElementById("privacy_text").innerHTML  = getName();
 });
</script>

</head>
<body>
<textarea id="privacy_text" class="terms">
</textarea>
</body>
</html>

Simply use $("#privacy_text").val(getName());

Working Fiddle

Use Like This

<script type="text/javascript" src="jquery.1.9.1.js"></script> 
<script type="text/javascript" src="First.js"></script> 

I did that using jquery. DEMO

 function callme(){ return 'success'; }
 <head> <script type="text/javascript" src="jquery.1.9.1.js"></script> <script type="text/javascript" src="first.js"></script> <script> $(document).ready(function(){ $("#privacy_text").val(callme()); }); </script> </head> <body> <textarea id="privacy_text" class="terms"> </textarea> </body>

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