Hello all, I am trying to update the .onclick event after its been clicked once. Everything works fine in my script except updating the .onclick event. I have searched the web and found that I should do the following:
div.onclick = function(params);
or
div.onclick = new function(){updatedStatement};
I have tested both of these steps in IE and FF with no luck. Here is my code:
function movePhrase(OPT,MENU,ID,DIR,LEVEL){
if(DIR == "up"){
var num = OPT - 1;
}else{
var num = OPT + 1;
}
var phraseID = "option" + OPT;
var newPhraseID = "option" + num;
var linkID = "link" + OPT;
var newLinkID = "link" + num;
var upImgID = "up" + OPT;
var downImgID = "down" + OPT;
var newUpImgID = "up" + num;
var newDownImgID = "down" + num;
if(OPT == 1){
var downDiv = document.getElementById(downImgID);
var newDownDiv = document.getElementById(newDownImgID);
var newUpDiv = document.getElementById(newUpImgID);
}else if(OPT == 0){
var upDiv = document.getElementById(upImgID);
var newUpDiv = document.getElementById(newUpImgID);
var newDownDiv = document.getElementById(newDownImgID);
}else{
var upDiv = document.getElementById(upImgID);
var downDiv = document.getElementById(downImgID);
var newDownDiv = document.getElementById(newDownImgID);
var newUpDiv = document.getElementById(newUpImgID);
}
var currentLink = document.getElementById(linkID);
var newLink = document.getElementById(newLinkID);
var linkURL = currentLink.href;
var newLinkURL = newLink.href;
var currentPhrase = document.getElementById(phraseID);
var newPhrase = document.getElementById(newPhraseID);
var text = currentPhrase.innerHTML;
var newText = newPhrase.innerHTML;
var params = "source=phrases&id="+ID+"&num="+OPT+"&move="+DIR+"&menu="+MENU+"&level="+LEVEL;
if (ID != "notSet"){
xhttp.open("POST","ajax.php",true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.setRequestHeader("Content-length", params.length);
xhttp.setRequestHeader("Connection", "close");
xhttp.onreadystatechange = function(){
var msgDiv = document.getElementById("message");
var msg = "Never Set";
if(xhttp.readyState == 4) {
msg = xhttp.responseText;
}else{
var readyState = xhttp.readyState;
msg = "Ready State: "+readyState+"<br />";
}
msgDiv.innerHTML = msg;
}
xhttp.send(params);
}
if(OPT == 1){
downDiv.onClick = newOnClcik(OPT,MENU,ID,'down',LEVEL);
newDownDiv.onClick = newOnClcik(num,MENU,ID,'down',LEVEL);
newUpDiv.onClick = newOnClcik(num,MENU,ID,'up',LEVEL);
}else if(OPT == 0){
upDiv.onclick = newOnClcik(OPT,MENU,ID,'up',LEVEL);
newUpDiv.onclick = newOnClcik(num,MENU,ID,'up',LEVEL);
newDownDiv.onclick = newOnClcik(num,MENU,ID,'down',LEVEL);
}else{
upDiv.onclick = newOnClcik(OPT,MENU,ID,'up',LEVEL);
downDiv.onclick = newOnClcik(OPT,MENU,ID,'down',LEVEL);
newDownDiv.onclick = newOnClcik(num,MENU,ID,'down',LEVEL);
newUpDiv.onclick = newOnClcik(num,MENU,ID,'up',LEVEL);
}
currentLink.href = newLinkURL;
newLink.href = linkURL;
currentPhrase.innerHTML = newText;
newPhrase.innerHTML = text;
}
function newOnClcik(opt,menu,id,level,dir){
var string = "movePhrase("+opt+","+menu+","+id+",'"+dir+"',"+level+")";
return string;
}
Below is an example or the div code I am trying to update:
<div id="downImage1">
<img id="down1" src="images/downArrow.JPG" onmouseover="this.src='images/downArrowPressed.JPG'" onclick="movePhrase(1,20,331,'down',1)" onmouseout="this.src='images/downArrow.JPG'" width="40" height="40" />
</div>
Any help would be appreciated.