www.webdeveloper.com
Results 1 to 11 of 11

Thread: Replacing One Image With Another Image

Hybrid View

  1. #1
    Join Date
    Oct 2013
    Posts
    20

    Replacing One Image With Another Image

    Hello JavaScript Masters!

    I'm pretty new to JavaScript, so forgive me, but I'm trying to replace an image with another image onMouseUp...and vice versa. Basically a play/pause button. I like using inline JavaScript, but if that's not possible then I understand.

    Since I am trying to learn more about JavaScript, I would welcome guidance on how to achieve this instead of just the code. However, I can usually figure stuff out if I mess with it, so if just posting the code is easier then I can probably still figure out how you did it.

    Your Humble Apprentice,

    -Bman

  2. #2
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    Here is an inline example:

    Code:
    <img src="image1.png" onmouseup="this.src = 'image2.png';" alt="" />
    "this" refers to the element that the event was triggered on, so IMG in this case.
    JavaScript: Learn | Validate | Compact | bionoid

  3. #3
    Join Date
    Oct 2013
    Posts
    20
    Bionoid,

    Thank you! How could I have been so dumb? For whatever reason I never tried to do it the image...only the <a> tag.

    Your code does change the image from the first to the second, but I also was hoping to be able to click it again an it go back to the first...and so on. Is that possible and still achieve the proper function of each button?

    You Humble Apprentice,

    -Bman

  4. #4
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    Before I change things to simply switch between images, are there other buttons that could also influence what state your play button is on.... like a "stop" button?

    [Play/Pause] [Stop] [Rewind] [Forward]

    Also, are there other events that need to be called based on each state? Like a startTrack or pauseTrack Javascript function.
    JavaScript: Learn | Validate | Compact | bionoid

  5. #5
    Join Date
    Oct 2013
    Posts
    20
    Yes. I am just making a simple Play, Pause, and Stop player where the play and pause are in the same place. So...

    [Play/Pause] [Stop]

  6. #6
    Join Date
    Oct 2013
    Posts
    20
    I have an interesting idea. I'd like your thoughts. I currently use an iframe with my audio element on it. My site runs completely in the iframe. I do this so that my music plays throughout the entire site without having to restart on ever page. Do you think it would be possible to have the player/controller be on my homepage but still be able to control the audio element in the parent iframe?

    I really appreciate your help thus far. I've already learned something new...which is why I came here.

  7. #7
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    I know I've gone a little overboard. If the audio part of your site is event driven, then you can trigger those events with your set of buttons.

    Added it as an attachment so you can see the goofy pictures I used as well
    Attached Files Attached Files
    JavaScript: Learn | Validate | Compact | bionoid

  8. #8
    Join Date
    Oct 2013
    Posts
    20
    Haha! I love them! Let me work on implementing that and I'll let you know if I have any further questions. Thank you so much for your help!

  9. #9
    Join Date
    Oct 2013
    Posts
    20
    Ok...so here's what I'm thinking. I will create a JS file with one function that contains the <audio> element and additional functions controlling it. I'll call the first function on my iframe and the other functions on my home page. Hopefully that will give me the result I'm looking for.

    What do you think?

  10. #10
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    I really don't know which is better. Personally I try not to use elements like <iframe>.
    If you want to keep the music going continuously, you could use AJAX to change the content of your pages instead.
    JavaScript: Learn | Validate | Compact | bionoid

  11. #11
    Join Date
    Oct 2013
    Posts
    20
    JavaScript was/is intimidating enough right now. I'm a beginner after all. HTML was pretty easy, but JavaScript is kind of kicking my butt right now. Maybe after I am proficient in that I can move on to AJAX and other stuff. I just don't think my brain can handle it right now. Haha!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles