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

setAttribute "value" returns undefined

I am dynamically (but not async) building html elements with js and encounter an error upon setting the "value" attribute.
Simplified code:

 let tag = document.createElement("div");
      tag.innerHTML = `${item}`;
      tag.setAttribute("value", `${item}`); 
      tag.classList.add("newtags", "lot of css classes")
      tagslist.appendChild(tag)


    newtags = Array.from(document.querySelectorAll('.newtags'))
    newtags.forEach(item => {
       item.addEventListener("click", e =>{
        console.log(e.target.value)
    })
  })

Upon clicking on the created tags I get "undefined" yet if I do console.log(e.target) without adding value I can see it is working and the value is there. Am I setting something that isn’t really the value of the element?
Thanks

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 :

div element doesn’t have value property. only few elements such as input, textarea have value
https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes

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