How to get the value of h1 tag using JS?

2020-07-17 06:22发布

问题:

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>

回答1:

Vanilla JavaScript solution (no framework required):

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


回答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>


回答3:

Use the class you've given the heading in normal js use the following.

var x = document.getElementsByClassName('entry-title').value;
console.log(x); //outputs collections trainer

If you're using jQuery then its

$('.entry-title').text

Hope that helps.



回答4:

If you want to get the h1 value only after the button click you will need to put the onclick for the button.

$.(document).ready(function(){
var header1Text ='';
 $('.application_button').onclick(function(){
   header1Text = $('h1').html();
  });
//To display it in whichever textbox you want:
$('#GivesomeIDtoYourTextBox').val(header1Text);
});

PS:You need to also include the jquery source to your page.



回答5:

jQuery: To get H1 value

 var gValue=jQuery("header h1.entry-title").text();
    alert(gValue);

As you need to fetch values on other pages, for that you can send values via QueryString or using HTML5 LocalStorage Sample Code:

var first_page_h1_Value=jQuery("header h1.entry-title").text();
var second_page_h1_Value=jQuery("header h1.entry-title").text();

localStorage.setItem("FirstPage", first_page_h1_Value);
localStorage.setItem("SecondPage", second_page_h1_Value);

On third Page you can get both pages header text

 alert(localStorage.FirstPage);
 alert(localStorage.SecondPage);


回答6:

If you are using Jquery then i would recommend that you give id to your H1 tag assuming id of your H1 tag is "h1Title". HTML:

<h1 id="h1Title">Heading here</h1>

Then to get the title you write following in JQuery:

var title = $("#h1Title").text();
//To Check Whether you are getting text or not
console.log("title :"+title);

By this you will get text of your heading.