Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

I thought this would be very simple, but I can't seem to get JS to write onto my HTML. Here is the simple code..

The button actually disappears, too.

<!DOCTYPE html>
<html>
    <head>
        <title>Write on Me</title>
        <script src="js/write.js" defer></script>
    </head>
    <body>

        <p id="paper" style="height:100px"></p>
        <button type="button" onclick="write();">Write something to the paragraph.</button>

    </body>
</html>
function write() {
    document.getElementById("paper").innerHTML="Here is some written text!";}

so I figured out if I rename the function something other than "write", it works. But I don't see anywhere on the web that that is a keyword.

5 Answers

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Hi Ryan Scharfer

This is a really interesting bug. I believe the onclick handler on the button is invoking the document.write() method and is overwriting the button element. For a quick test change <button type="button" onclick="write();"> to <button type="button" onclick="window.write();"> and you'll see that it works. The write() method isn't strictly a JavaScript reserved word -- it doesn't have anything to do with the JavaScript language, but it is a keyword that's used by the browser, in particular the document object.

it has been a while since I used js, but if I remember correctly ' write' is a key word used within the DOM document group. Proper usage would be as;

document.write('text in here writes directly to screen');

or you can pass variables

x = 'some text';

document.write (x);

would output 'some text'

It is definitely behaving like a keyword in my program, but I can't find any list of keywords that includes it. Strange.

:)

as I said, it has been a while since I have used any of this. Thought I would take these courses to refresh my memory, I don't even know where to point you too.

This site does refer to it though http://www.webteacher.com/javascript/

Thanks Dave. I just wanted to make sure I wasn't crazy. : )