Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to change the onclick event

Status
Not open for further replies.

pahiker

Programmer
Aug 31, 2012
45
US
For the most part it works, but the text to be sent via the onclick is held in an array. When the code executes it transfers the name of the array, not it's contents. How do I fix that?


e.onclick = function() {update(ary[l][0]);};

Let all bear in mind that a society is judged not so much by the standards attained by its more affluent and privileged members as by the quality of life which it is able to assure for its weakest members.
 
Show us how you are attempting to access the value inside the update() function.
Is the value inside ary[l][0] also an array or just a text value?

As vague as the question and code is, the best I can say, is the following general example works as expected.

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]
<html>
<head>
<title>change href</title>
<script type="text/javascript">
var myvar = new Array();
myvar[0] = new Array();

myvar[0][0]='entry1';
myvar[0][1]='entrey2';


function myfunc(theValue)
{
	alert(theValue);
}


</script>
</head>


<body onload="document.getElementById('mybut').onclick = function(){myfunc(myvar[0][1]);};">
<input type="button" id="mybut" value="Click Me"></button>

</body>
</html>

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web &amp; Tech
 
Apologies, other places I've been told I put too much in. Here is the entire JS


function update(btn) {
var ary = new Array();


switch (btn) {
case "Home":
document.getElementById("Content").src = "Home/Prayer.html";
document.getElementById("Chapter").innerHTML = "We are an organization bringing disabled and<br>non-disable people together through sports.";

ary[0] = new Array("Home/AboutUs.html", "About Us", "HomeButton");
ary[1] = new Array("Home/News.html", "Latest News", "HomeButton");
ary[2] = new Array("Home/Sponsor.html", "Sponsor Friends to Friends!", "HomeButton");
ary[3] = new Array("Home/JeffersonAwards.html", "George O'Donnell receives the Jefferson Award", "HomeButton");
ary[4] = new Array("Home/MiracleField.html", "Miracle Field Calendar", "HomeButton");
ary[5] = new Array("Home/Contact.html", "Contact Us", "HomeButton");
break;


case "SwPa":
document.getElementById("Chapter").innerHTML = "Southwest Pennsylvania Chapter<br><br>";
ary[0] = new Array("update('Home')", "Calendar", "Waiting");
ary[1] = new Array("SwPa/PhotoAlbum.html", "Photo Albums", "Waiting");
break;


case "NwPa":
document.getElementById("Chapter").innerHTML = "Northwest Pennsylvania Chapter<br><br>";
ary[0] = new Array("NwPa/Calendar.html", "Calendar", "Waiting");
ary[1] = new Array("NwPa/PhotoAlbum.html", "Photo Albums", "Waiting");
break;
}


var allTags = document.getElementsByTagName("*");
var i=0, l=0;
var e;
while(e=allTags[i++]){
if(e.id.substring(0,4) == "link") {
if (l < ary.length) {
e.onclick = function() {update(ary[l][0]);};
e.innerHTML = ary[l][1];
e.setAttribute("class", ary[l][2]);
l++;
alert(e.onclick);
}
else {
e.setAttribute("class", 'Trans');
}
}
}

return;
}


Let all bear in mind that a society is judged not so much by the standards attained by its more affluent and privileged members as by the quality of life which it is able to assure for its weakest members.
 
Still not sure I follow the issue. And I can say the way this is done is quite convoluted, and not very optimal. You are looping through every single element in the page to find some links apparently.

Anyway, the loop will pass pass the first value ( [0] ) of the " l " array that's inside the ary array to your update function.

Basically what you will be getting is: "Home/AboutUs.html" or "Home/News.html" etc... But your switch is checking for values that will never be passed to it "Home", "SwPa", etc...

So its seems your update() function never builds the new arrays. Where should the values "Home" or "SwPa" etc.. be coming from?










----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web &amp; Tech
 
Found the solution.

The problem is that the onclick was being filled with ary[0][1] instead of the contents of the array. The solution was to use:

e.onclick = new Function() {update(ary[l][0]);};

This filled the onclick with what the array contained.

Let all bear in mind that a society is judged not so much by the standards attained by its more affluent and privileged members as by the quality of life which it is able to assure for its weakest members.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top