I have a function that changes the innerHTML of a DIV element.
I want to pass to that function as a parameter a reference to the DIV to be changed.
In the example below it works the first time (executed with the body onLoad event). As the subsequent function declaraton is rewritten by the function itself, it can no longer reference the targeted DIV.
I tried to pass a string teference and use eval(), but this time it fails the third time it is executed. ?
[CODE]
<html>
<head>
<title>Passing Parameters to a Function (with object prameter)</title>
<script type=”text/javascript”>
function startSlideshow(){
psCreateCtrl(1, 5, document.getElementById(“ps”));
}
function psCreateCtrl(psSelectedImg, psFrameShift, psDiv){
// PARAMETERS:
// psSelectedImg – defines which image is to be selected initially
// psFrameShift – how many buttons are displayed around the selected image
// psDiv – a reference to the DIV that will be assigned the generated code
var psStartPos; //the position of the first image button to be displayed
var psEndPos; //the position of the last image button to be displayed
var psCtrl = “”; //holds the generated HTML code
var psDatabase = “one|two|three|four|five|six|seven|eight|nine|ten|so on…|||||||||||||||||||||”.split(“|”);
// defines the start position of the image sequence:
psStartPos = psSelectedImg – psFrameShift;
if (psStartPos + psFrameShift*2 > psDatabase.length){ //increases the displayed images number to the LEFT of the slected image (reduces psStartPos)
psStartPos = psDatabase.length -psFrameShift*2;
}
if(psStartPos <1){ // start position cannot be < 1
psStartPos=1;
}
// defines the start position of the image sequence:
psEndPos = psSelectedImg + psFrameShift;
if(psEndPos – psStartPos != psFrameShift*2){ //increases the displayed images number to the LEFT of the slected image (increases psEndPos)
psEndPos = psStartPos + psFrameShift*2;
}
if(psEndPos > psDatabase.length){ // end position cannot be > psDatabase.length
psEndPos=psDatabase.length;
}
//generates the hyperlinks for the clickable image sequence buttons
for (i=psStartPos; i<=psEndPos; i++){ //the link buttons
if(psSelectedImg != i){
psCtrl += ‘<A title=”Photo ‘ + i + ‘” href=”javascript: psCreateCtrl(‘ + i + ‘, ‘ + 5 + ‘, ‘ + psDiv + ‘);”>’ + i + ‘</A>’;
}
else { //the selected image red inactive button
psCtrl += “<span class=”psSelImg”>” + i + “</span>”;
}
}
psDiv.innerHTML = psCtrl;
}
</script>
<style type=”text/css”>
<!–
#ps {
CLEAR: both;
font-family: Verdana, Geneva, sans-serif;
font-size: 10px;
margin: 5px;
position: absolute;
z-index: 100;
}
#ps A {
TEXT-ALIGN: center;
PADDING-BOTTOM: 0px;
PADDING-LEFT: 3px;
PADDING-RIGHT: 3px;
DISPLAY: block;
FLOAT: left;
COLOR: #D8CB9E;
TEXT-DECORATION: none;
PADDING-TOP: 0px;
width: 22px;
margin-top: 0px;
margin-right: 5px;
margin-bottom: 0px;
margin-left: 0px;
border: 1px solid #9E8F5A;
background-color: #36F;
}
#ps A:hover {
BORDER-BOTTOM-COLOR: #9E8F5A;
BORDER-TOP-COLOR: #192A3A;
COLOR: #192A3A;
BORDER-RIGHT-COLOR: #9E8F5A;
BORDER-LEFT-COLOR: #192A3A;
background-color: #C4B78E;
}
#ps .psSelImg {
width: 22px;
TEXT-ALIGN: center;
PADDING-BOTTOM: 0px;
MARGIN: 0px 5px 0px 0px;
PADDING-LEFT: 3px;
PADDING-RIGHT: 3px;
DISPLAY: block;
FLOAT: left;
COLOR: #D8CB9E;
TEXT-DECORATION: none;
PADDING-TOP: 0px;
border: 1px solid #BDB38C;
cursor: default;
background-color: #C00;
}
–>
</style>
</head>
<body onLoad=”startSlideshow()”>
<div id=”ps”></div>
</body>
</html>