Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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 Attached Thumbnails Make a h1 class innerHTML a variable-untitled-3.jpg  

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,983
    Thanks
    56
    Thanked 557 Times in 554 Posts
    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; 
    	}
    }

  • Users who have thanked xelawho for this post:

    MikeWalters (10-11-2012)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,525
    Thanks
    80
    Thanked 4,490 Times in 4,454 Posts
    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.

  • Users who have thanked Old Pedant for this post:

    MikeWalters (10-11-2012)

  • #4
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,983
    Thanks
    56
    Thanked 557 Times in 554 Posts

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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •