PDA

View Full Version : how to to pass array[x] to a variable?



ksquared3
02-07-2011, 02:41 AM
I want to pass array[x] to a variable so that I can use the variable in .innerHTML. I've researched and not found a reference.

Any help appreciated. Big thanks.

Nile
02-07-2011, 02:48 AM
Do you mean how can you pass it in a function?


(function(value){
document.getElementByid('element').innerHTML = value;
}(array[x]));

ksquared3
02-07-2011, 02:55 AM
ok, let me show you my function that doesn't work:


function captionToDiv() {
document.getElementById("captionHere").innerHTML = "captionsList[caption]";
return false;
}

can we start here?

Nile
02-07-2011, 02:58 AM
function captionToDiv() {
document.getElementById("captionHere").innerHTML = captionsList[caption];
return false;
}

Should work, aslong as you make sure captionsList is defined before you call captionToDiv(). If you want to pass it as a parameter:


function captionToDiv(val) {
document.getElementById("captionHere").innerHTML = val;
return false;
}
captionToDiv(captionsList[caption]);

ksquared3
02-07-2011, 03:16 AM
The function to create and fill the array runs before I call captionToDiv().

Did you mean for me to use the code you have highlighted?

Nile
02-07-2011, 03:20 AM
Ignore the highlighted, I was just pointing out the variable.

ksquared3
02-07-2011, 03:24 AM
here is the computed code which returns error: missing ) after formal parameters. Pointing to line 1.


function captionToDiv(captionsList[caption]) {
document.getElementById("captionHere").innerHTML = "captionsList[caption]";
return false;
}

I don't see where ")" is needed.

Nile
02-07-2011, 03:28 AM
No - change your code to:


function captionToDiv() {
document.getElementById("captionHere").innerHTML = captionsList[caption];
return false;
}

ksquared3
02-07-2011, 03:46 AM
receiving error: captionsList not defined. It is defined in the <head> and also in the function that creates the array, and also here:

function captionToDiv() {
var captionsList;
document.getElementById("captionHere").innerHTML = captionsList[caption];
return false;
}

am I missing something?

Nile
02-07-2011, 03:50 AM
Take the var captionList; away. Paste all of your code on your page here please.

ksquared3
02-07-2011, 03:57 AM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php $thisPage="fullPainting"; ?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="styles.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
<!--
var paintingBack = 1;
var paintingNext = 1;
var caption = 1;
var captionsList;
var pointback;
var backToDiv;
var pointnext;
var nextToDiv;
function processback() {
pointback();
backToDiv();
captionToDiv();
}
function pointback(){
if (paintingBack > 1) {
paintingBack = paintingBack-1;}
return;
}
function backToDiv(){
document.getElementById("paintingImage").innerHTML = "<img src=\"images/painting_"+paintingBack+".jpg\" />";
paintingNext = paintingBack;
caption = paintingBack;
alert("linksays "+caption);
return false;
}
function processnext() {
pointnext();
nextToDiv();
captionToDiv();
}
function pointnext(){
if (paintingNext < 49){
paintingNext++;}
return;
}
function nextToDiv(){
document.getElementById("paintingImage").innerHTML = "<img src=\"images/painting_"+paintingNext+".jpg\" />";
paintingBack = paintingNext;
caption = paintingBack;
return false;
}
function captionToDiv() {
document.getElementById("captionHere").innerHTML = captionsList[caption];
return false;
}
//-->
</script>
<title>STEPHANIE PEEK</title>
</head>
<body>
<div id="center" align="center">
<div id="container">
<div id="main">
<?php $paintingNumber = $_REQUEST['paintingNumber']; ?>
<script type="text/javascript">
paintingBack = "<?php echo $paintingNumber; ?>";
paintingNext = "<?php echo $paintingNumber; ?>";
caption= "<?php echo $paintingNumber; ?>";
</script>
<script type="text/javascript">
function captionsToCaptionsList() {
var captionsList;
try{
txtFile = new XMLHttpRequest();
}
catch(e){
try{
txtFile = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e) {
try{
txtFile = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
alert("Ajax not supported by your browser!");
return false;
}
}
}
txtFile.open("GET", "paintinglist.txt", true);
txtFile.onreadystatechange = function() {
if (txtFile.readyState === 4) {// Makes sure the document is ready to parse.
if (txtFile.status === 200) {// Makes sure it's found the file.
captionsList = txtFile.responseText;
lines = txtFile.responseText.split("\n");//Will separate each line into an array
}
}
}
txtFile.send(null);
}
</script>

<?php include ("includes/topNav.php"); ?>
<div class="clear"></div>
<div class="painting" id="paintingImage"><img src='images/painting_<?php echo $paintingNumber; ?>.jpg' /></div>
<div class="captionText" id="captionHere"></div>
<div class="clear"></div>
</div><!--end main -->
</div><!--end container -->
</div><!--end center -->
</body>
</html>]

include:
[code] <a href="" class='link' onFocus='if(this.blur)this.blur()' onclick="processback(); return false;"/><img src='images/braceL.gif' alt='brace' width='20' height='11' valign='text-bottom'/></a>
<a href="thumbnails.php" class="linkCurrent" target="_self" onFocus="if(this.blur)this.blur()" />PAINTINGS</a>
<a href="" class='link' onFocus='if(this.blur)this.blur()' onclick="processnext(); return false;"/><img src='images/braceR.gif' alt='brace' width='20' height='11' valign='text-bottom'/></a>

Nile
02-07-2011, 03:59 AM
<script type="text/javascript">
function captionsToCaptionsList() {
var captionsList;
Change to:


<script type="text/javascript">
var captionsList;
function captionsToCaptionsList() {

ksquared3
02-07-2011, 04:12 AM
computed code with error: captionsList is undefined pointing to line beginning with "document.get....."


function captionToDiv() {
document.getElementById("captionHere").innerHTML = captionsList[caption];
return false;
}

ksquared3
02-07-2011, 04:48 AM
changed "var captionsList" to "var captionsList = " " and that worked. No errors. Thank you!

However,

document.getElementById("captionHere").innerHTML = captionsList[caption];

The html page is displaying "undefined" in place of captionsList[caption]

ksquared3
02-07-2011, 07:13 AM
Thank you so much for your help. There was a problem in the code that created and filled the array. Now, it seems to be working fine.

Nile
02-07-2011, 12:51 PM
No problem, I'm glad to help :D

Here on DD, we like to keep things organized. In an effort to do so, you have the option to set a thread to resolved when an issue is fixed. To make the status of the thread resolved:
1. Go to your first post
2. Edit your first post
3. Click "Go Advanced"
4. In the dropdown next to the title, select "RESOLVED"