treehouse : what would you like to learn today?
Web Design Web Development iOS Development

How to hide images in html

  • I have this script which switches images,

    <script type="text/javascript">
    window.onload = function () {

    var header = document.getElementById('header');
    var pictures = new Array('images/image1.jpg','images/image2.jpg','images/image3.jpg','images/image4.jpg','images/image5.jpg');
    var numPics = pictures.length;
    if (document.images) {
    var chosenPic = Math.floor((Math.random() * numPics));
    header.style.background = 'url(' + pictures[chosenPic] + ')';

    }
    }
    </script>


    Unfortunately the script switches background images but the sprites which are linked to from the html stay on top and are not replaced. I need to find a way to replace the existing images or hide them.

    <div id="contentright">

    <div id="artcontainer">
    <div id="art-boxes">
    <a class="row1-1" href="#"></a>
    <a class="row1-2" href="#"></a>
    <a class="row1-3" href="#"></a>
    <a class="row1-4 norightmargin" href="#"></a>

    <a class="row2-1" href="#"></a>
    <a class="row2-2" href="#"></a>
    <a class="row2-3" href="#"></a>
    <a class="row2-4 norightmargin" href="#"></a>

    <a class="row3-1" href="#"></a>
    <a class="row3-2" href="#"></a>
    <a class="row3-3" href="#"></a>
    <a class="row3-4 norightmargin" href="#"></a>
    </div><!-- END ARTBOXES -->
    </div><!-- END ARTCONTAINER -->

    </div><!--END CONTENTRIGHT-->


    Is there some JS that I can add to the script to tell it to randomly hide the sprites e.g. artcontainer = 'hide'
  • What do you mean by "randomly" hide?
    You could go:
    artcontainer.style.display = 'none';

    I'm not exactly sure what the problem is - Perhaps I'm reading it incorrectly. Also are you intentionally trying to avoid jQuery? (I'm asking because my javascript capabilities are embarrassing lol)
  • I am assuming that the 'art-boxes' contains thumbnailed versions of the random backgrounds? When the page loads, choose a random background, and hide it from the list of other backgrounds? A kind of reverse gallery selection?

    Again I'm with @jamy_za and although I understand your script, if it was written in jQuery it would seem to read more semantically.

    artcontainer.style.display = 'none';
    // vs
    $('#artcontainer').hide();


    I also find it easier to draft out ideas - even if the script itself is not valid (I would have at least broken the procedure down!):

    $('#header').css('background-image', picture-url); //'picture-url' being the result of the math;
    $('#artcontainer a[href="'+ picture-url'"]).hide(); //Find the link to the same image, and hide it;


    The code hasn't been tested, so may not work - but even then you can see the method I was aiming for?
  • Thanks for getting back to me, it's too late for me to look at this now and my brain is fried. Anyway I have put up a very basic cleaned out version of the problem. As you can see the script changes out the background images on refresh, but the sprite css rollovers remain on top and visible even though they are also background images. I would like them to appear randomly on refresh as the other background images do, but I don't know how to write it into the script. I'm not exactly a Jquery or JS Jenius.

    P.S. Later.....I tried out both of your solutions, but obviously I didn't explain the problem correctly.
  • Brain was definitely fried last night, here's the link to the cleaned down version
    http://emasai.com/depinho/jstrial.html
  • So you want to rearrange the a elements randomly ?

    well with jquery try:
     $(document).ready(function(){
    var rows = 3;
    var cols = 4;
    var arr = new Array();
    function get_class() {
    var r = Math.ceil(Math.random()* rows);
    var c = Math.ceil(Math.random()* cols);
    var cssclass = 'row'+r+'-'+c;
    if($.inArray(cssclass, arr) < 0){
    arr.push(cssclass);
    return cssclass;
    } else {
    return get_class();
    }
    }
    $('#art-boxes a').each(function(i,e){
    $(this).removeClass($(this).attr('class').match(/row\d-\d/gi));
    $(this).addClass(get_class());
    });
    });


    I havn't testet it, so don't blame me ^^

  • Thanks for anwering, but that is not at all what I want to do.
    There are 5 background images which show on Refresh, my problem is that they show up behind those 12 boxes, I want each of the 5 images to show up and then have the 12 boxes show up as if it were the 6th image.
  • Ok thats easy ^^


    <script type="text/javascript">

    window.onload = function () {
    var header = document.getElementById('header');
    var pictures = new Array('images/image1.jpg','images/image2.jpg','images/image3.jpg','images/image4.jpg','images/image5.jpg','art-boxes');
    var numPics = pictures.length;
    if (document.images) {
    var chosenPic = pictures[Math.random() * numPics];

    if(chosenPic == 'art-boxes'){
    document.getElementById(chosenPic).style.display = 'block';
    }else{
    header.style.background = 'url(' + chosenPic + ')';
    }
    }
    }
    </script>


    and html


    <div id="contentright">

    <div id="artcontainer">
    <div id="art-boxes" style="display:none;">
    <a class="row1-1" href="#"></a>
    <a class="row1-2" href="#"></a>
    <a class="row1-3" href="#"></a>
    <a class="row1-4 norightmargin" href="#"></a>

    <a class="row2-1" href="#"></a>
    <a class="row2-2" href="#"></a>
    <a class="row2-3" href="#"></a>
    <a class="row2-4 norightmargin" href="#"></a>

    <a class="row3-1" href="#"></a>
    <a class="row3-2" href="#"></a>
    <a class="row3-3" href="#"></a>
    <a class="row3-4 norightmargin" href="#"></a>
    </div><!-- END ARTBOXES -->
    </div><!-- END ARTCONTAINER -->

    </div><!--END CONTENTRIGHT-->


    Hope thats want you want ;)
  • Thanks for trying, I understand the logic of it of what you are doing, but unfortunately none of the images shows up at all now.
  • So, you want the five background images to alternate in front of the boxes, while this happens the boxes will not be visible. Then, after the five images, the boxes show up. Is that right? Now, if you want the background images to show in front of the boxes you will have to actually put them as an image in front of the boxes with absolute positioning and a higher z-index(all this with css). then have a function change the image in front of the boxes in an interval that you specify like setInterval(). Also, when the function runs through all the images, just place an empty source and nothing will show up, therefore leting the person see the blocks... Sorry if it doesn't make sense, but I wrote this in a real big rush.. Hope this helps...
  • Damn sorry i messed up

    change:

    var randnum = Math.floor(Math.random() * numPics);
    var chosenPic = pictures[randnum];


    After some sleep i will test the whole thing.
  • Well i should read it twice after writing code without testing it or: copy/pasting is bad :P
    There is no element with the id of 'header' in your dom..
    Change also:
    var header = document.getElementById('contentright');
  • Thank you guys so much for taking a look, I'm just reading this quickly on Superbowl Sunday, so I will have another trial at it tomorrow with a fresh look and get back to you to let you know the results.