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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Posts
    408
    Thanks
    17
    Thanked 2 Times in 2 Posts

    is this script opener right? getting element class

    The script inside my opening if statement is working, but is the rest of this written right? I'm just want my script to run after it makes sure that a div class named single-journal-entry-wrapper is on the page.

    Code:
    function doit() {
    	
    	document.getElementsByTagName("div")
    	for ( var x = 0; x < div.length; x++ )
           {
               var div = div[x];
               if ( div.className.indexOf("single-journal-entry-wrapper") >= 0 )
                    { do somethang!}
           }
    }
    
    
    {

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    Looks right to me.

    You might want to add break; at the end of your "do domehting" code, else if there is more than one <div> with that class name the "do something" code will execute once for each such <div>. The break; says "exit the surrounding for loop".
    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.

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    408
    Thanks
    17
    Thanked 2 Times in 2 Posts
    hmmm... the break; didn't make a difference. I did find a jquery solution that works:

    if($('div').hasClass('single-journal-entry-wrapper')) {do something}

    But as I learn more, that has me wondering what the problems with using jquery are. I know it's obviously cheating me out of learning to code properly. But other than that?

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    Teach me not to read carefully!

    You omitted a very important variable assignment and then misused a variable.
    Code:
    <html>
    <head>
    <script>
    function doit() 
    {
        // you MUST assign the collection of <div>s to a variable!
        var divs = document.getElementsByTagName("div");
        // you must search *IN* that collection!
        for ( var x = 0; x < divs.length; x++ )
        {
            // and must get one member of that collection:
            var div = divs[x];
            if ( div.className.indexOf("single-journal-entry-wrapper") >= 0 )
            { 
                alert("Found this: " + div.innerHTML );
                // try un-commenting next line to see the difference
                // break; 
            }
        }
    }
    window.onload = doit;
    </script>
    </head>
    <body>
    <div class="whompit">
       should not find this
    </div>
    <div class="whompit single-journal-entry-wrapper">
       should find this first
    </div>
    <div class="single-journal-entry-wrapper">
       should ALSO find this if you omit the break
    </div>
    </body>
    </html>
    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.


  •  

    Posting Permissions

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