How to get the value of h1 tag using JS?

33,867

Solution 1

Vanilla JavaScript solution (no framework required):

var h1Text = document.querySelector(".entry-title").textContent;

Solution 2

can you use jquery? if so, get the h1 values when you click the button from jquery and then sent to the other page using query string.

EDITED

Add the jquery file in your page to user jquery features. And you need to put the function inside $(document).ready() function in order to attach the function into the object.

you can learn more about jquery in https://learn.jquery.com/.

<script src="https://code.jquery.com/jquery-2.2.0.min.js"/>
<script>
  $(document).ready(function(){
   $('.application_button').click(function(){
        var headervalue = $(".entry-title").text();
        window.location = "http://homecredit.ph/testEnvironment/4537-2/?position="+headervalue;
    });
});
</script>
Share:
33,867
User014019
Author by

User014019

:)

Updated on June 11, 2021

Comments

  • User014019
    User014019 almost 3 years

    I have 3 pages, the 2 pages are WordPress pages and the other 1 is a custom page template with a form. The 2 pages are created using wp-job manager plugin. The 1st page has had a dropdown menu and contains list of jobs. On the 2nd page is the description of a job.

    Now, I want to get the value of h1 tag on the 2nd page after the user click the input button and pass it to the 3rd page and displayed it in one of the input textbox (Position input textbox) using JS.

    How to do this?

    Here's the link of the 2nd page
    3rd page

    HTML:

    <header class="entry-header">
        <h1 class="entry-title">Collections Trainer</h1>
    </header>
    
  • User014019
    User014019 over 8 years
    i will put that jquery on the 2nd page or 3rd page?
  • Mg Thar
    Mg Thar over 8 years
    2nd page. And then get the value from query string at the 3rd page.
  • User014019
    User014019 over 8 years
    I put that script on the 2nd page, but when I click on the button, nothing happens and it's not redirecting
  • Mg Thar
    Mg Thar over 8 years
    Did you put the script inside the document.ready function? And did you included the jquery source?
  • User014019
    User014019 over 8 years
    i got an alert "undefined"
  • Prafful Panwar
    Prafful Panwar almost 4 years
    $('.entry-title').text(); you missed the Round brackets.