3

I'm trying to learn Javascript and decided to try the innerHTML property myself. But the browser seems to ignore the Javascript code. What am I doing wrong?

 <!DOCTYPE html>
    <html>
      <head>
        <title>JavaScript - Document Object Model </title>

        <meta name="viewport" content="width=device-width, initial-scale=1.0">
      </head>
      <body>
        <div id="page">
          <h1 id="header">List</h1>
          <h2>Buy groceries</h2>
          <ul id="todo">
            <li id="one" class="hot"><em>fresh</em> figs</li>
            <li id="two" class="hot">pine nuts</li><li id="three" class="hot">honey</li>
            <li id="four">balsamic vinegar</li>
         </ul>
       </div>

     <script type="text/javascript">
       var thirdItem = getElementById('three');
       var itemContent = thirdItem.innerHTML;
       thirdItem.innerHTML = '<li id="three" class="hot">chicken</li>';
     </script>
   </body>
  </html>
4
  • 2
    It's document.getElementById() - you have to call it as a method of the document object. Commented Oct 4, 2015 at 13:34
  • 1
    Also you're trying to put an <li> inside the <li> that's already there - the replacement text should just be "chicken". Commented Oct 4, 2015 at 13:35
  • try check errors in browser console Commented Oct 4, 2015 at 13:39
  • Thank you! Much appreciated. Commented Oct 4, 2015 at 14:40

1 Answer 1

5

Your code should be:

var thirdItem = document.getElementById('three');
...
thirdItem.innerHTML = 'chicken';

'You might not need jQuery' is a short reference of the JavaScript DOM routines vs. jQuery ones.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.