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 01-26-2013, 03:07 AM   PM User | #1
juliushg
New Coder

 
Join Date: Jul 2012
Location: Mexico
Posts: 45
Thanks: 9
Thanked 0 Times in 0 Posts
juliushg is an unknown quantity at this point
I need to change a series of values

I have a list of divs like this:
Code:
<div>8</div>
<div>5</div>
<div>0</div>
<div>6</div>
<div>0</div>
<div>0</div>
<div>1</div>
Now I need to turn off all zeroes displaying, I suppose that it's something like:

Code:
if (document.getElementsByTagname("div").innerHTML="0"){
**then the innerHTML changes to " " **
}
But I'm not sure the exact steps, please enlighten me.
juliushg is offline   Reply With Quote
Old 01-26-2013, 03:14 AM   PM User | #2
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
Do you *MEAN* turn off? In that case, instead of just changing the 0 to "" wouldn't you want to change the div's style to "display: none;"??

BUt anyway:
Code:
var divs = document.getElementsByTagname("div");
for ( var d = 0; d < divs.length; ++d )
{
    var div = divs[d];
    if ( div.innerHTML == "0" ) 
    {
        div.innerHTML = "";
        // or 
        div.style.display = "none";
    }
}
Note that this will process *ALL* the <div>s on your page.

If possible try to somehow "contain" them.

Example:
Code:
<div id="onlyLookHere">
    <div>8</div>
    <div>5</div>
    <div>0</div>
    <div>6</div>
    <div>0</div>
    <div>0</div>
    <div>1</div>
</div>
and then do:
Code:
var divs = document.getElementById("onlyLookHere").getElementsByTagname("div");
for ( var d = 0; d < divs.length; ++d )
{
    var div = divs[d];
    if ( div.innerHTML == "0" ) 
    {
        div.innerHTML = "";
        // or 
        div.style.display = "none";
    }
}
__________________
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:
juliushg (01-26-2013)
Old 01-26-2013, 04:05 AM   PM User | #3
juliushg
New Coder

 
Join Date: Jul 2012
Location: Mexico
Posts: 45
Thanks: 9
Thanked 0 Times in 0 Posts
juliushg is an unknown quantity at this point
Thumbs up

Yes! It worked great, I only observed one typo ("Tagname") and because I'm not used to method names, it was bugging me, but I found it. And yes, both hide methods work for me. And yes, I had to contain the divs in another one.

Thanks Old Pedant, how long since the last time you helped me!
juliushg 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 02:13 PM.


Advertisement
Log in to turn off these ads.