Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use in html body a variable that's been defined in script in the head part

In the following code how can I correctly insert the val testName defined in the function initialize() in the field Name in the body?

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.min.js"></script>
<script type="text/javascript">

function intialize()
{
    var testName="John";
}
</script>
</head>

<body onload="intialize()">
<input id="Name" type="textbox" value=testName>
</body>
</html>
like image 579
Ash Avatar asked Jun 20 '11 06:06

Ash


1 Answers

You can use document ready :

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.min.js"></script>
<script type="text/javascript">

$(function(){
      $('#Name').val('testName');
});

</script>
</head>

<body>
<input id="Name" type="textbox" value=""/>
</body>
</html>

Used the id, Name, and set the value to 'testName' once the document is ready.

like image 58
Marc Uberstein Avatar answered Oct 12 '22 23:10

Marc Uberstein