Results 1 to 4 of 4

Thread: onClick to change image but with a variable passed

  1. #1
    Join Date
    Jul 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default onClick to change image but with a variable passed

    I have the simplest js function to change an image on a page -

    function swapImage() {
    this.document.IMG1.src = 'propertyimages/PWI07071360/slides/image1.jpg'
    return(false);
    }

    which works perfectly. The problem is I need the path to the image to contain a php variable. I tried this

    function swapImage(thisImage) {
    this.document.IMG1.src = 'propertyimages/' + thisImage + '/image2.jpg'
    return(false);
    }

    Where thisImage is passed as the following - PWI07071360, but it's not working at all. Am I building the string incorrectly in the function?

    I also tried this -

    function swapImage(thisImage) {
    newImageName = 'propertyimages/' + thisImage + '/slides/image2.jpg';
    this.document.IMG1.src = newImageName
    return(false);
    }

    I'd welcome any help or suggestions please.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    function swapImage(thisImage) {
      document.images['IMG1'].src = "propertyImages/" + thisImage + "/slides/image2.jpg";
    }
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Jul 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks. That works perfectly now.
    (I almost passed a second parameter to specify the image number from 1 to 9 but instead I went with 9 separate javascript functions. That way the .php looks way more complex and my boss is way more impressed).

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Haha, oh dear. You may have earned the respect of your boss, but I think at the cost of the derision of any other web developers who encounter your code
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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