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 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2012
    Location
    Mexico
    Posts
    45
    Thanks
    9
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,189
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    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.

  • Users who have thanked Old Pedant for this post:

    juliushg (01-26-2013)

  • #3
    New Coder
    Join Date
    Jul 2012
    Location
    Mexico
    Posts
    45
    Thanks
    9
    Thanked 0 Times in 0 Posts

    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!


  •  

    Posting Permissions

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