JavaScript Toggle paragraph display using JavaScript

Can someone tell me why the JavaScript code below is returning an undefined error when I click on the button the second time?

When clicked the first time, it will reveal the text in the h3 element, and the button's inner.text will change. However, when I click it again, I get the undefined error.

I look in the DOM and see that the class name has changed to "reveal" after the first click and it does. I assumed on the second click, when the class name was changed to "reveal", the conditional statement would jump to the "else" portion and change the class back to "hide" and button text back in order to hide the h3 text again. However, I get the "Uncaught TypeError: Cannot read property 'className' of undefined".

<body>
  <header>
    <h1>Header</h1>
    <div id="button-div">
        <button id="button">Click to Reveal</button>
    </div>
    <h3 class="hide">Text</h3>
    <h2>subtext</h2>

  </header>
</body>

CSS:

.hide {
  display: none;
}

And the JS:

var button = document.getElementById("button");
var textHide = document.getElementsByClassName("hide");

button.addEventListener("click", function() {
    if (textHide[0].className === "hide") {
      textHide[0].className = "reveal";
      button.innertext = "Click to Hide";
    } else {
      textHide[0].className = "hide";
      button.innertext = "Click to Reveal";  
    }
    console.log("click");
});
Answer:1

You got the error "Uncaught TypeError: Cannot read property 'className' of undefined" because there is no element with class name 'hide'on second click.

var button = document.getElementById("button");
var textHide = document.getElementsByTagName("h3");

button.addEventListener("click", function() {
    if (textHide[0].className == "hide") {
      textHide[0].className = "reveal";
      button.innertext = "Click to Hide";
    } else {
      textHide[0].className = "hide";
      button.innertext = "Click to Reveal";  
    }
    console.log("click");
});

Demo:

http://jsfiddle.net/5pq0hbye/

Answer:2

It happens because getElementsByClassName works in a live manner. So, when you change the className from hide to none, the object textHide is also updated, and then there are no more elements with the hide CSS class in your page.

You can avoid that behavior by using querySelectorAll:

var textHide = document.querySelectorAll(".hide");
Answer:3

I recently moved from callback functions to promises in node.js. I want to preform async query to the DB (psql) in the most elegant way. I was wondering if the following code is the right way to do it ...

I recently moved from callback functions to promises in node.js. I want to preform async query to the DB (psql) in the most elegant way. I was wondering if the following code is the right way to do it ...

  1. node chain promise
  2. node promise chain error handling
  3. node promise chain example
  4. node js chain promise
  5. node js promise chain example
  6. node break promise chain
  7. node js promise chain catch
  8. node js promise chain error
  9. node js break promise chain
  10. node.js exit promise chain
  11. node.js request-promise chain

For websites that rely on Ajax or javascript to render data, how can I use WWW::Selenium to save the data? My following code was able to perform all the clicks and get to the correct webpage, however ...

For websites that rely on Ajax or javascript to render data, how can I use WWW::Selenium to save the data? My following code was able to perform all the clicks and get to the correct webpage, however ...

  1. website builder
  2. website design
  3. website maker
  4. website hosting
  5. website templates
  6. website checker
  7. website speed test
  8. website citation
  9. website builder free
  10. website name generator
  11. website down
  12. website unblocker
  13. website domain
  14. website apa citation
  15. website in spanish
  16. website archive
  17. websites like youtube
  18. website traffic
  19. website ideas
  20. websites like airbnb

I have a requirement in a username field where I have to validate the special characters but allow '.' dot character. We have custom method in plugin alphanumeric but it doesn't allow dot. please ...

I have a requirement in a username field where I have to validate the special characters but allow '.' dot character. We have custom method in plugin alphanumeric but it doesn't allow dot. please ...

  1. jquery validate plugin not working
  2. jquery validate plugin example
  3. jquery validate plugin cdn
  4. jquery validate plugin download
  5. jquery validate plugin tutorial
  6. jquery validate plugin before submit
  7. jquery validate plugin error message placement
  8. jquery validate plugin date format mm/dd/yyyy
  9. jquery validate plugin add rules dynamically
  10. jquery validate plugin select box
  11. jquery validate plugin float number
  12. jquery validate plugin
  13. jquery validate plugin email
  14. jquery validate plugin phone number
  15. jquery validate plugin rules
  16. jquery validate plugin regex
  17. jquery validate plugin documentation
  18. jquery validate plugin date
  19. jquery validate plugin checkbox
  20. jquery validate plugin submithandler

I am trying to create a Google Adwords script that can somehow exclude TODAY's date from the Date Range. According to Adwords documentation the general syntax is as follows: forDateRange("20130324", "...

I am trying to create a Google Adwords script that can somehow exclude TODAY's date from the Date Range. According to Adwords documentation the general syntax is as follows: forDateRange("20130324", "...