Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Newbie: hanging browser on function call

Tags:

javascript

I just started learning JavaScript and am wondering why this simple snippet hangs when I click on the "Call function" button. What am I missing?

<html>
<head>

<script type="text/javascript">
function myfunction()
{
document.write("hello");
}
</script>

</head>
<body>

<form>
<input type="button" 
onclick="myfunction()" 
value="Call function">
</form>

</body>
</html>
like image 457
RexE Avatar asked Feb 28 '23 23:02

RexE


1 Answers

You need to write inside an element or give an element a value, or you should use document write like that :

<html>
<head>

<script type="text/javascript">
function myfunction()
{
document.getElementById("lblMessage").innerText = "hello";
document.getElementById("txtMessage").value = "hello";
//document.write("hello");
}
</script>

</head>
<body>

<form>

<script type="text/javascript">
document.write("This is written while page processed by browser !<br />");
</script>

<input type="text" id="txtMessage" /><br />
<span id="lblMessage"></span><br />
<input type="button" 
onclick="myfunction()" 
value="Call function">
</form>

</body>
</html>
like image 54
Canavar Avatar answered Mar 12 '23 23:03

Canavar