Trouble implementing click event

Hello,

I am trying to change the visibility property of an element to ‘visible’ when clicked and have tried numerous ways without finding the solution.

The latest attempts was to assign a variable to that property, which would then be used in a function in the click event.

The output is that the properties of addEventListener cannot be read (‘null’).

I would be grateful for some input as I have been trying for a while now, which is probably making it hard to see mistakes also.

Thanks a lot.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="styles.css" />
    
    <title>Pol</title>
  </head>
  <body>
    
      <header>
        <h1>Polyglottery</h1>
      </header>
   
<div class = "nav_plus_bios">
    <nav id="navbar" class="navbar">
      <ul>
        <li>Quicklinks</li>
        <li><a href="#james_joyce">James Joyce</a></li>
        
        <li><a href="#natalie_portman">Natalie Portman</a></li>
        
        <li><a href="#jrr_tolkien">J.R.R. Tolkien</a></li>
      
        <li><a href="#shakira">Shakira</a></li>
      </ul>
    </nav>


    <section class = "main_section_excl_nav">
        <p id="intro_text" class="intro_text">
            CHANGE LATER
          </p>
          
            <div class="james_joyce_container">
              
              <img src = "Images/jamesjoyce.jpg" alt="Image of James Joyce">
             
    
    
      <!--<img src="Images/jamesjoyce.jpg" class = "james_joyce_img" />-->

      <p class = "james_joyce_biography" id = "james_joyce_biography">
      <strong>James Joyce</strong> (1882-1941) The famous Irish writer was
        known for his love of languages and literature. He was initially a
        teacher and a translator; he taught English all over Europe and during
        his travels he was able to pick up a variety of other languages,
        including Italian, German, and French. It is hard to determine how many
        he actually spoke, but he’s most often credited with communicating in
        between 13 and 17 other languages. His Ulysses contains fragments in
        Hebrew, Latin, Greek, Spanish, Irish Gaelic, and more. Apart from that,
        he was also a scholar who studied the nature of languages, so he could
        probably read a wider variety of texts. Apparently he only learned
        Norwegian to read Ibsen in his original language.
      </p>

    
    
  </div> 
  <!--jj buttn below
  <button onclick="hideShow()class="james_joyce_button">Click to read about James Joyce</button>-->
  <button class = "james_joyce_button" id = 'james_joyce_button'>James Joyce</button>
</div>
<footer>
 
</footer>
</section>
<script src="script.js"> </script>
  </body>
</html>

(css)

.james_joyce_biography {
  visibility: hidden;
}

script:

let visibility = window.getComputedStyle(james_joyce_biography).visibility;


const james_joyce_button = document.querySelector("james_joyce_button");



james_joyce_button.addEventListener("click", function () {
  if ((visibility = "hidden")) {
    visibility = "visible";
  }
});

Hi! Can you show your full CSS?

hi there, sure, no problem:

* {
  margin: 0px;
}
body {
  margin-right: 50px;
  background-color: rgb(212, 230, 249);
}

.james_joyce_container,
.natalie_portman_container,
.jrr_tolkien_container,
.shakira_container {
  display: flex;
  gap: 40px;
  margin-bottom: 80px;
  color: rgb(70, 69, 69);
  /*border: solid 6px rgba(5, 90, 164, 0.9)*/
  /*width: fit-content;*/
  /*block-size: fit-content;*/
  margin-left: 100px;
  margin-right: 150px;
  background-color: rgb(181, 224, 247);
  padding: 20px;
}

/*
.james_joyce_button {
  background-image: url(Images/jamesjoyce.jpg);
}
.natalie_portman_button {
  background-image: url(Images/natalie_portman.jpg);
}
.jrr_tolkien_button {
  background-image: url(Images/jrr_Tolkien.png);
}
.shakira_button {
  background-image: url(Images/shakira.jpg);
}*/

.james_joyce {
  height: 200px;
}
.natalie_portman {
  height: 200px;
}

.jrr_tolkien {
  height: 200px;
}

.shakira {
  height: 150px;
}

h1 {
  color: rgb(193, 96, 96);
  font-size: 80px;

  position: fixed;
  left: 0;
  right: 0;
}

header {
  height: 120px;
  width: 90vw;
  background-color: rgb(32, 2, 139);
  position: fixed;
  border-bottom: solid 7px rgb(52, 41, 244);
  border-radius: 10px;
  padding: 15px;
  margin: auto;

  text-align: center;

 
}

ul {
  list-style: none;
}


.intro_text {
  color: rgb(70, 69, 69);
  margin-left: 55px;
  margin-right: 150px;
  margin-top: 150px;
  margin-bottom: 50px;
  font-size: 18px;
  
  padding: 50px;
}
a {
  color: rgb(70, 69, 69);
}

.nav_plus_bios {
  display: flex;
  margin-top: 0px;
}

.main_section_excl_nav {

  margin-right: -70px;
  margin-top: 0px;
}
li {
  padding-top: 20px;
  padding-left: 0px;
  padding-right: 80px;
}
footer {
  height: 500px;
  background-color: rgb(32, 2, 139);
}
a:hover {
  color: black;
  background-color: blueviolet;
}
a:visited {
  color: grey;
}

.navbar {
  margin-top: 125px;
  background-color: rgb(240, 237, 179);
}


.james_joyce_biography {
  visibility: hidden;
}

.james_joyce_button {
  margin-left: 50px;
}

Thanks! Well, but what is your intention? You want to click on hidden button to make it visible? Some strange logic. Or you want to click on the button and get the hidden text, maybe?

I want the text in the (currently) hidden ‘p’ element to become visible when the button is clicked.

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

here you are assigning, not checking

querySelector requires a css selector, you need to have the . for class or # for id

Yes, it has sense, so here is my solution using visibility:

Add onclick Event

<button class="james_joyce_button" id='james_joyce_button' onclick="toggleText()">James Joyce</button>

And the function

function toggleText() {
    let text = document.getElementById("james_joyce_biography");
    if (text.style.visibility === "hidden") {
        text.style.visibility = "visible";
    } else {
        text.style.visibility = "hidden";
    }
}

I would have a class like

.hidden {
   visibility: hidden;
   display: none;
}

and do

element.classList.toggle('hidden')

this will toggle (add or remove) the class to/from the element, so you don’t need to know the state before

of course. thanks, have updated now

thanks, have updated to
const james_joyce_button = document.querySelector(".james_joyce_button");

my own code i still not working with these two updated changes though.

what issue are you having?

i have now targetted the class correctly (using a " . ") and have replaced = with " == " ,but the text still remains hidden when I click the button

this is not going to change anything, you are assigning to a variable, not making any changes to the element

You want the element to be visible, you need to select it and change the css
I suggested this

Ray13 suggested a different way

i will try. i made those changes as they were suggested first

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.