Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 7th, 2006, 05:40 PM
Registered User
 
Join Date: Mar 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default problem with swapping image into a div

i have a bunch of links on my main page
i.e
a href="one/index.html"
a href="two/index.html"
etc.
i only want to go to the index target if javascript is disabled...if it isn't, i'd like it so when i click the link i want to swap an image in a div (id="theimage") on the page.

here's what i've tried:

function prepareMainLinks() {
    var mainImages = Array()
    mainImages["one"] = "pics/a.jpg";
    mainImages["two"] = "dummy/b.jpg";
    mainImages["three"] = "tack/c.jpg";
    mainImages["four"] = "pics/joe.jpg";
    mainImages["five"] = "words/anotherword.jpg";

    // graceful degradation tests
    if (!document.getElementsByTagName || !document.getElementById || !document.getElementById("topmenu")) return false;
    // set up all the links to call showPic
    var topmenu = document.getElementById("topmenu");
    var links = topmenu.getElementsByTagName("a");
    var errorstring;
    for (var i = 0; i < links.length; i++) {
        var the_link = links[i];
        var the_links_href = the_link.getAttribute("href");
        if (the_links_href) {
            for (key in mainImages) {
                if (the_links_href.indexOf(key) == 0) {
                    the_link.onclick = function() {
                        return !(showPic("theimage", mainImages[key]));
                    }
        }
    }
}


function showPic(location, new_pic) {
    if (!document.getElementById || !document.getElementById(location)) return false;
    var where = document.getElementById(location);
    where.setAttribute("src", new_pic);
    return true;
}


problem is, it only shows the last image (anotherword.jpg) for all the links..i'm assuming there's a problem in my onclick function, i'm assuming mainImages[key] get's evaluated too late...at which point they're all set to the last image...

how do i fix this?
 
Old April 8th, 2006, 04:52 PM
Registered User
 
Join Date: Mar 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i've done this to get it to work, but I'm still wondering why have mainImages as a global didn't work, if anyone knows I'd appreciate hearing why.

function prepareMainLinks() {
    mainImages = Array();
    mainImages["evacuee"] = "evacuee/pics/evacusaurus.jpg";
    mainImages["dummy"] = "dummy/comingsoon.jpg";
    mainImages["tack"] = "tack/tack.jpg";
    mainImages["rvid"] = "rvid/rvid.jpg";
    mainImages["words"] = "words/husky.jpg";
    // graceful degradation tests
    if (!document.getElementsByTagName || !document.getElementById || !document.getElementById("topmenu"))
        return false;
    // set up all the links to call showPic
    var topmenu = document.getElementById("topmenu");
    var links = topmenu.getElementsByTagName("a");
    for (var i = 0; i < links.length; i++) {
        var the_link = links[i];
        var the_links_href = the_link.getAttribute("href");
        for (key in mainImages) {
            if (the_links_href.indexOf(key) == 0) {
                the_link.associatedImageSrc = mainImages[key];
                the_link.onclick = function() {
                    return !(showPic("theimage", this.associatedImageSrc));
                    //old approach that didn't work
                    //return !(showPic("theimage", mainImages[key]));
                }
            }
        }
    }
}





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to center text/image/any object into a div beetle_jaipur BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 3 January 24th, 2013 01:25 PM
image cuts through div Adam H-W CSS Cascading Style Sheets 0 October 13th, 2007 04:10 PM
Background Image problem in DIV tag. cumminsj CSS Cascading Style Sheets 6 September 15th, 2006 03:02 AM
background image for <div> anshul CSS Cascading Style Sheets 4 May 16th, 2005 11:00 AM
Swapping Images software_developer_kk Javascript How-To 1 April 13th, 2005 10:00 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.