Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the value of an <h2> tag with JavaScript

I am trying to generate an MD5 check-sum of every element on a page that has the <h2> tag, then display the values as a popup.

The code I have already should get each <h2> element, I just need to get the actual value of each one.

var ghead = document.getElementsByTagName('h2');

for (i=0; i<ghead.length; i++) {
    var gh = ghead[i];
    var ts = gh.toString();
    var ms = b64_md5(ts);
    alert(ts);
    alert(ms);
}

The usage of b64_md5(ts) basically is what converts the ts variable into the MD5 value. However, the ts variable is the ID or name of the Type of Element, not the Element's value itself.

Also, if I wanted to make a cookie that has two values stored in it, a name and a checksum, could I use gh.innerText; to set the unique name, as I have had issues with using this method so far.

like image 646
JamEngulfer Avatar asked Jul 03 '12 12:07

JamEngulfer


People also ask

How to count the number of H2 tags in a page?

When you click the Count H2 button, the page shows the number of H2 tags Use JavaScript getElementsByTagName () with getAttribute () method to allow select an element by its tag name and attribute.

How to get element by its tag name in JavaScript?

5: JavaScript Get Element by Attribute. Use JavaScript getElementsByTagName () with getAttribute () method to allow select an element by its tag name and attribute. The following syntax represents the attribute () method: 1. elem = document.getElementsByTagName ("input") [0].getAttribute ("class");

How to get HTML elements values by attributes in JavaScript?

Javascript access the dom elements by id, class, name, tag, attribute and it’s valued. Here you will learn how to get HTML elements values, attributes by getElementById (), getElementsByClassName (), getElementByName (), getElementsByTagName (). The following javascript dom method help to select the elements in document.

How to get the textual content of a H2 Tag element?

To get the textual content of a h2 tag element gh: var text = gh.childNodes.item(0).data; Share Improve this answer Follow answered Jul 3 '12 at 13:07 Michael BesteckMichael Besteck 2,2751515 silver badges88 bronze badges


1 Answers

You can use the innerHTML property to get the HTML contents of an element:

var ts = gh.innerHTML;

Note that h2 elements (and most other elements) don't have a "value". Only elements that behave as form controls have a value property (e.g. the input element).

like image 103
James Allardice Avatar answered Sep 24 '22 10:09

James Allardice