Jump to content

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

In that onclick, "write" refers to document.write, not window.write (which is what your function actually is).

Try both:

onclick="window.write()"

document.write = function() { alert("document.write"); };

...onclick="write()"...

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.