Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-11-2012, 02:28 AM   PM User | #1
MikeWalters
New to the CF scene

 
Join Date: Oct 2012
Posts: 2
Thanks: 2
Thanked 0 Times in 0 Posts
MikeWalters is an unknown quantity at this point
Question Make a h1 class innerHTML a variable

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.
Attached Thumbnails
Click image for larger version

Name:	Untitled-3.jpg
Views:	32
Size:	44.7 KB
ID:	11618  
MikeWalters is offline   Reply With Quote
Old 10-11-2012, 03:02 AM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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; 
	}
}
xelawho is offline   Reply With Quote
Users who have thanked xelawho for this post:
MikeWalters (10-11-2012)
Old 10-11-2012, 03:05 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,162
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
MikeWalters (10-11-2012)
Old 10-11-2012, 03:20 AM   PM User | #4
MikeWalters
New to the CF scene

 
Join Date: Oct 2012
Posts: 2
Thanks: 2
Thanked 0 Times in 0 Posts
MikeWalters is an unknown quantity at this point
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.
MikeWalters is offline   Reply With Quote
Old 10-11-2012, 03:31 AM   PM User | #5
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough

I forget some browsers don't need incessant hand-holding...
xelawho is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:40 PM.


Advertisement
Log in to turn off these ads.