admin管理员组

文章数量:1316974

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>

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>
Share Improve this question asked Jun 13, 2014 at 12:59 VinodVinod 2,31111 gold badges61 silver badges106 bronze badges 13
  • 1 Look in your browser's JavaScript console. Read the error message. Ask yourself where you expect $ to e from. – Quentin Commented Jun 13, 2014 at 13:01
  • 1 you using jQuery(document).ready(fu... where is you jquery CDN link <script src="jquery.1.9.1"></script – kamesh Commented Jun 13, 2014 at 13:02
  • 1 I have included that, wait I will edit my question – Vinod Commented Jun 13, 2014 at 13:02
  • 2 Textareas have a .value, not a .innerHTML. – Niet the Dark Absol Commented Jun 13, 2014 at 13:03
  • 2 .innerHTML also works jsfiddle/sfarsaci/me9yh – Totò Commented Jun 13, 2014 at 13:05
 |  Show 8 more ments

2 Answers 2

Reset to default 3

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>

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> 

本文标签: How to get return value from javascript to htmlStack Overflow