Jump to content

Doesn't run the function?


3raser

Recommended Posts

Here is my code:

 

<html>
<head>
<title>Hey there, punk!</title>
</head>
<body>

<script type="text/javascript">

function write() {
alert("hey");
}
</script>

<form>
<input type="button" value="TouchMe" onclick="write()">
</form>

</html>
</html>

 

Whenever the button is clicked, the alert box doesn't show up.

Link to comment
https://forums.phpfreaks.com/topic/232721-doesnt-run-the-function/
Share on other sites

write isn't exactly a reserved word in Javascript

http://www.webdevelopersnotes.com/tutorials/javascript/reserved.php3

 

Though it should be.. maybe that's an old post or something.  Only way to find out is to use a different name for your function.

it's onClick.. not onclick

 

EDIT: well, I thought it was case-sensitive.

Try setting a proper DOCTYPE

 

Only XHTML is case-sensitive, but in that case it would be written 'onclick' anyway.

 

Edit:

 

Also it depends on the context in which the event is assigned; from within a window.onload event for example, it would refer to window.wirte(). If defined within the document, for example during the onclick HTML event, then it would refer to document.write(). So this would work (not 100% on browser compatibility):

 

<input type="button" value="TouchMe" onclick="write('hey');">

 

But it's not exactly great JavaScript. As requinix said though, changing it to window.write() will work.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.