[Javascript] How to separate the href

Scott Moore moore.scott at gmail.com
Thu Aug 11 10:04:55 CDT 2005


Triche, thanks so much for your help.

The reason I wanted to pull the image name out of the onclick  
function is so that those who have JavaScript turned off would still  
be able to view the large version of the image.

I thought it might be as easy as changing
document.getElementById('placeholder').src = whichpic;
to
document.getElementById('placeholder').src = whichpic.href;

since it worked in the script I originally started with
function showPic (whichpic) {
     if (document.getElementById) {
         document.getElementById('placeholder').src = whichpic.href;
         return false;
         } else {
         return true;
     }
}

However, that's not the case and the image href acts like a normal link.

Any suggestions?


On Aug 11, 2005, at 10:02 AM, Triche Osborne wrote:

Scott Moore wrote:

> I'm using this script...
>
>> ...which gives the CSS class of selected to a thumbnail when it's
>>
> clicked and displays the larger version of the image where   
> id=placeholder is defined, works  as intended when called using  
> this  link format.
> <a onclick="showPic(this,'img01.jpg')"></a>
> What I'm trying to figure out is how to modify the script to allow   
> the href to be defined outside the onclick, like this:
> <a onclick="showPic(this)" href="img01.jpg"></a>
>
Not quite sure what you're aiming for here. If you do this, natural  
XHTML anchor behavior will open img01.jpg on a blank page the way it  
would any normal link. If that's all you want to do, you don't need  
the script; if that's not what you intended, perhaps you could be a  
bit more specific about what behavior you're trying to achieve by  
divorcing the image name from the onclick function.


> I'm also trying to figure out how to have the first thumbnail link   
> have the class=selected when the page first loads.
>
Assign an ID attribute (I used "first" for the example below) to your  
first thumbnail, then alter your script, thus:

var Ary=new Array();
var firstSelected;

window.onload = function() {
firstSelected = document.getElementById('first');
firstSelected.className = "selected";
}

function showPic (obj,whichpic) {

     if (firstSelected.className == "selected"){
     firstSelected.className = "notselected";
     }

     if (!obj.set){
      obj.set=true;
      Ary[Ary.length]=obj;
     }

     for (i=0;i<Ary.length;i++){
      Ary[i].className='notselected';
     }

     obj.className='selected';
         if (document.getElementById) {
                 document.getElementById('placeholder').src = whichpic;
         }

}

Triche

_______________________________________________
Javascript mailing list
Javascript at LaTech.edu
https://lists.LaTech.edu/mailman/listinfo/javascript





More information about the Javascript mailing list