Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the value of h1 tag using JS?

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>
like image 293
User014019 Avatar asked Jan 27 '16 09:01

User014019


People also ask

How do you find the value of h tag?

document. getElementById(). innerHTML.

What is h1 in JavaScript?

Definition and Usage The <h1> to <h6> tags are used to define HTML headings. <h1> defines the most important heading. <h6> defines the least important heading.

How can you grab the h1 element to change the text inside it?

You should use the textContent property as a best practice. The innerHTML property changes everything inbetween the h1 tags, which could mean that if there was something else between the h1 tags, like an emphasis or strong tag it would disappear. textContent would only change the text of the header.

How will you create a h1 tag in JavaScript?

We can create a new h1 element with JavaScript by using the createElement() method along with the innerHTML property. var newh1 = document. createElement("h1"); newh1. innerHTML = "This is a new h1 element."


2 Answers

Vanilla JavaScript solution (no framework required):

var h1Text = document.querySelector(".entry-title").textContent;
like image 188
Matías Fidemraizer Avatar answered Nov 14 '22 21:11

Matías Fidemraizer


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>
like image 25
Mg Thar Avatar answered Nov 14 '22 22:11

Mg Thar