Jump to content

Just doesn't work(Changing images)


ebolt007

Recommended Posts

Hello;

 

I'm new to Javascript and I'm missing something here in my code. I'm trying to have my vendor_pic_main image change to the smaller images as they are clicked, but just can't get it to work. Can anyone help me with this?  Here is my code, I'm loading the images from a database, and that's where I get really confused.

 

    <?
    $sql = "SELECT * FROM Vendors WHERE VendorID ='$bridal_id'";

$query_result = mysql_query($sql);

$Content_row = mysql_fetch_assoc($query_result);

$image01 = $Content_row['Image1FileName'];
$image02 = $Content_row['Image2FileName'];
$image03 = $Content_row['Image3FileName'];
$image04 = $Content_row['Image4FileName'];
$image05 = $Content_row['Image5FileName'];
$image06 = $Content_row['Image6FileName'];

    echo "<div class=\"vendor_pic_main\"><img src=\"images/vendors/$image01\" id=\"im\" ALT=\" \" ></div>"."\n"; 
    echo '<div class="vendor_pic_small_holder"> '."\n";
     echo " <div class=\"vendor_pic_small\"><a href=\"\" onclick=\"document.getElementById('im').src='images/vendors/$image01\'\"><img src=\"images/vendors/$image01\" width=\"40\" height=\"40\" ALT=\" \" ></a></div> "."\n";		
      echo " <div class=\"vendor_pic_small\"><a href=\"\"  onclick=\"document.getElementById('im').src='images/vendors/$image02\'\"><img src=\"images/vendors/$image02\" width=\"40\" height=\"40\" ALT=\" \" ></a></div> "."\n";		
      echo " <div class=\"vendor_pic_small\"><a href=\"\"  onclick=\"document.getElementById('im').src='images/vendors/$image03\'\"><img src=\"images/vendors/$image03\" width=\"40\" height=\"40\" ALT=\" \" ></a></div> "."\n";	
      echo " <div class=\"vendor_pic_small\"><a href=\"\"  onclick=\"document.getElementById('im').src='images/vendors/$image03\'\"><img src=\"images/vendors/$image04\" width=\"40\" height=\"40\" ALT=\" \" ></a></div> "."\n";		
      echo " <div class=\"vendor_pic_small\"><a href=\"\"  onclick=\"document.getElementById('im').src='images/vendors/$image03\'\"><img src=\"images/vendors/$image05\" width=\"40\" height=\"40\" ALT=\" \" ></a></div> "."\n";	
      echo " <div class=\"vendor_pic_small\"><a href=\"\"  onclick=\"document.getElementById('im').src='images/vendors/$image03\'\"><img src=\"images/vendors/$image06\" width=\"40\" height=\"40\" ALT=\" \" ></a></div> "."\n";	
     echo '</div>   '."\n";
    ?>

 

The images show up fine, the clicking and changing images just doesn't work.

Link to comment
Share on other sites

What JS errors are you getting? You should have something in your error console. Also, why are you putting a backslash before the closing single quote on your image source in your onclick?

onclick=\"document.getElementById('im').src='images/vendors/$image01\'\"

should be

onclick=\"document.getElementById('im').src='images/vendors/$image01'\"

 

Link to comment
Share on other sites

Awesome thanks taking off the \ after the $image worked to change the image, didn't even notice that.  but it only changes for a second then it changes right back to the original $image01 rather than staying on like $image03.

 

Here's what my code now looks like. It seems like it reloads the page and that is when the $image01 reappears after switching to the other image. Hope that makes sence?

 

  <?
    $sql = "SELECT * FROM Vendors WHERE VendorID ='$bridal_id'";

$query_result = mysql_query($sql);

$Content_row = mysql_fetch_assoc($query_result);

$image01 = $Content_row['Image1FileName'];
$image02 = $Content_row['Image2FileName'];
$image03 = $Content_row['Image3FileName'];
$image04 = $Content_row['Image4FileName'];
$image05 = $Content_row['Image5FileName'];
$image06 = $Content_row['Image6FileName'];
         
    echo "<div class=\"vendor_pic_main\"><img src=\"images/vendors/$image01\" id=\"im\" ALT=\" \" ></div>"."\n"; 
    echo '<div class="vendor_pic_small_holder"> '."\n";
      echo " <div class=\"vendor_pic_small\"><a href=\"\"  onclick=\"document.getElementById('im').src='images/vendors/$image01'\"><img src=\"images/vendors/$image01\" width=\"40\" height=\"40\" ALT=\" \" ></a></div> "."\n";		
      echo " <div class=\"vendor_pic_small\"><a href=\"\"  onclick=\"document.getElementById('im').src='images/vendors/$image02'\"><img src=\"images/vendors/$image02\" width=\"40\" height=\"40\" ALT=\" \" ></a></div> "."\n";		
      echo " <div class=\"vendor_pic_small\"><a href=\"\"  onclick=\"document.getElementById('im').src='images/vendors/$image03'\"><img src=\"images/vendors/$image03\" width=\"40\" height=\"40\" ALT=\" \" ></a></div> "."\n";	
      echo " <div class=\"vendor_pic_small\"><a href=\"\"  onclick=\"document.getElementById('im').src='images/vendors/$image04'\"><img src=\"images/vendors/$image04\" width=\"40\" height=\"40\" ALT=\" \" ></a></div> "."\n";		
      echo " <div class=\"vendor_pic_small\"><a href=\"\"  onclick=\"document.getElementById('im').src='images/vendors/$image05'\"><img src=\"images/vendors/$image05\" width=\"40\" height=\"40\" ALT=\" \" ></a></div> "."\n";	
      echo " <div class=\"vendor_pic_small\"><a href=\"\"  onclick=\"document.getElementById('im').src='images/vendors/$image06'\"><img src=\"images/vendors/$image06\" width=\"40\" height=\"40\" ALT=\" \" ></a></div> "."\n";	
     echo '</div>   '."\n";
    ?>

Link to comment
Share on other sites

Ah, yes I believe the reason is that you have an empty href attribute. You need to add # to it. Like this:

<a href=\"#\"  onclick=\"document.getElementById('im').src='images/vendors/$image01'\"><img src=\"images/vendors/$image01\" width=\"40\" height=\"40\" ALT=\" \" ></a>

 

Otherwise, I believe it just defaults to whatever the current URL is, so clicking on one of those links would reload the page without the #.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.