[Javascript] How to separate the href

Triche Osborne wdlists at triche-osborne.com
Thu Aug 11 10:02:55 CDT 2005


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




More information about the Javascript mailing list