Not signed in (Sign In)

SkillShare - A place to discuss Web Standards and Web Design topics

Categories

Vanilla 1.1.9 is a product of Lussumo. More Information: Documentation, Community Support.

    • CommentAuthorjay0316
    • CommentTimeApr 10th 2008 edited
     permalink
    This seems like it should be an easy thing to do, but I'm having a ridiculously hard time doing it. When all is finished, I'd like to have 2 links/buttons. When you click button 1 it will put a rounded corner orange square behind it to show it's active. When you click button 2, it will turn off button 1's square and display the square behind button 2. I have four images I'm working with. 2 with just the text, and 2 with the text and the square behind it. Like I said it seems like it should be easy, however, so far everyone of the snippets I've tried will not swap an image when I click on it. I've got to be doing something obviously wrong.

    Can someone help me?

    Here is one of the code snippets I came across:
    <SCRIPT LANGUAGE=JavaScript>
    function swapImage() {
    var image = document.getElementByID("swap")
    image.src = "button2 image path"
    }
    </SCRIPT>

    <IMG id="swap" src="button1 image path" onclick="swapImage();" border="0" />
  1.  permalink
    For starters, its document.getElementById, not document.getElementByID
    • CommentAuthorjay0316
    • CommentTimeApr 10th 2008
     permalink
    Wow! That is a good start! The image now swaps once when I click it. Where would I start in getting it to behave the way I described?

    Thanks.
    • CommentAuthorjay0316
    • CommentTimeApr 10th 2008
     permalink
    Well, here is what I've done for now. I created a function for each button. Is this an acceptable way of doing this?

    <SCRIPT LANGUAGE=JavaScript>

    function swapDirections() {
    var image = document.getElementById("directionsLink")
    image.src = "directions2.png"
    var image2 = document.getElementById("scienceLink")
    image2.src = "science1.png"
    }
    function swapScience() {
    var image = document.getElementById("scienceLink")
    image.src = "science2.png"
    var image2 = document.getElementById("directionsLink")
    image2.src = "directions1.png"
    }
    </SCRIPT>

    <IMG id="directionsLink" src="directions1.png" onclick="swapDirections();" border="0" />
    <IMG id="scienceLink" src="science1.png" onclick="swapScience();" border="0" />
  2.  permalink
    I'd believe so. Sure, you could get by with only a single function, but if you're just beginnning javascript this is a good start. You might want to use a bit more modern tag for the script, though. Like this:

    You might also want to take a look at CSS sprites, demonstated and discussed at ALA.

    That might not be what you are looking for, but using a single file with both images has the advantage of at least appearing to load faster when the user makes the click. You would basically be moving the background position on both items instead of loading a new image into them, but as I said, this might be something that's not necessary at this point.
    • CommentAuthorjay0316
    • CommentTimeApr 11th 2008
     permalink
    Hey thanks for the links and feedback. When you say that I may want to use more modern tags, do you mean replacing the image.src with the css position? In other words, is modifying the src what's out of date?

    I'm glad I'm on the right track at least.

    Thanks!
  3.  permalink
    Damn... I meant the script tags that are missing the charset="utf-8" and a type="text/javascript" attributes. Naturally, it got clipped away from the comment. Also, I think the language attribute is deprecated.
    • CommentAuthorjay0316
    • CommentTimeApr 11th 2008 edited
     permalink
    Oh, Ok. I will look that up and make sure I get that straightened out. Thanks Again!
Add your comments
    Username Password
  • Format comments as (Help)