CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   Make a h1 class innerHTML a variable (http://www.codingforums.com/showthread.php?t=276080)

MikeWalters 10-11-2012 02:28 AM

Make a h1 class innerHTML a variable
 
1 Attachment(s)
Using:
Greasemonkey
Firefox 11.0
----------------
I was creating a script but I came to a halt. I am trying to make a title from a movie on neflix to a variable.
(see attachment)
(I am going to use the movie Hannibal as an example)
Code:

var netflixTitle = document.title.substring(document.title.length);
I tried this code but I get undefined. I've searched forums about h1 tags and found some but they only have solutions for
Code:

<h1 id="title">This is what I want</h1>
NOT
Code:

<h1 class="title">This is what I want</h1>
Thanks goes out for the guy that helps an ametuer out.:thumbsup:

xelawho 10-11-2012 03:02 AM

I've never used greasemonkey, but in normal javascript you'd do something like this:

Code:

var tags=document.getElementsByTagName("H1");
for (var i = 0; i < tags.length; i++) {
if(tags[i].className=="title"){
var thetitle=tags[i].innerHTML;
        }
}


Old Pedant 10-11-2012 03:05 AM

Since you are using Firefox:
Code:

var theTitle = document.getElementsByClassName("title")[0].innerHTML;
This assumes that there is only one element with class="title" or at least that the one you want is the first such.

If that's not the case, we could do it by finding that class nested within the <div> with id="displaypage-overview-details". Thus;
Code:

var theTitle = document.getElementById("displaypage-overview-details")
              .document.getElementsByClassName("title")[0]
              .innerHTML;

Xelawho: Works, but why do it? Since he is using FF you *know* that getElementsByClassName is available.

MikeWalters 10-11-2012 03:20 AM

Thank you Old Pedant, the
Code:

var theTitle = document.getElementsByClassName("title")[0].innerHTML;
worked like a charm. Also thank you Xelawho. I really appreciate it guys.

xelawho 10-11-2012 03:31 AM

:thumbsup:
I forget some browsers don't need incessant hand-holding...


All times are GMT +1. The time now is 10:39 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.