Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How could I have a button display innerHTM text once it's clicked

How could I make a button to respond by displaying the content inside saved in local storage



//store value once entered
 localStorage.setItem('store', JSON.stringify(ids1.value);
 let local = localStorage.getItem('store');

let button = document.getElementsByClassName('button');
let newd = document.getElementsByClassName('newd');


let parog = document.createElement('p');
parog.innerHTML = local;
parog.style.display = 'none';

if(local !== null){ 

    button.addEventListener('click', () => { 
        newd[0].appendChild(parog.style.display = 'block');
       
    })
}

What I get is addEventListener is not a function. Any idea on how to get ti display id the condition is met

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

getElementsByClassName returns a list of elements.

Either do:

button[0].addEventListener('click', () => { 
  newd[0].appendChild(parog.style.display = 'block');
})

or

let button = document.querySelector('.button');

(This is assuming your buttons have the class button. If you’re trying to query for all button elements, use document.getElementsByName("button") or document.querySelector("button"))

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading