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 09-09-2011, 07:24 PM   PM User | #1
turpentyne
Regular Coder

 
Join Date: Aug 2010
Posts: 312
Thanks: 10
Thanked 1 Time in 1 Post
turpentyne is an unknown quantity at this point
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!}
       }
}


{
turpentyne is offline   Reply With Quote
Old 09-09-2011, 07:53 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 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
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.
Old Pedant is offline   Reply With Quote
Old 09-09-2011, 08:11 PM   PM User | #3
turpentyne
Regular Coder

 
Join Date: Aug 2010
Posts: 312
Thanks: 10
Thanked 1 Time in 1 Post
turpentyne is an unknown quantity at this point
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?
turpentyne is offline   Reply With Quote
Old 09-09-2011, 09:17 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 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
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.
Old Pedant 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 04:21 PM.


Advertisement
Log in to turn off these ads.