Search code examples
javascripthtmlhandlebars.js

Getting a HTML h1 value to Javascript variable


If I wanted the value of a title that is going to be assigned to a header(h3) to become a javascript variable to bring information out of the local storage on a specific entry, how would I do that?


Solution

  • It really depends on your use-case what would be the best way to do that, and if you provide a little more code, the community might be better positioned to help you. In general, you can access the content of the first h3 tag by using:

    document.getElementsByTagName('h3')[0].innerHTML
    

    or if your tag has an id so you can use the below one

    document.getElementById('yourId').innerHTML
    

    or, if you have access to jQuery: $('h3').text()