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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing the onchange value

Status
Not open for further replies.

Merkaba

Programmer
Jun 14, 2005
69
US
Hey, I'm writing some code to display a rank in a select box. I have to make it so the same number isn't picked twice. My code works the first time, but I have to change the onchange (ironic I know) to have the new rank as origRank. The code is like this:

Code:
function changeRank(changedID, origRank)
	{	alert('so far');
		var rankDropDown = document.getElementsByTagName("select");
		var thisRank = document.getElementById(changedID);
		
		for (var i = 0; i < rankDropDown.length; i++)
		{
		
			if (origRank > thisRank.options[thisRank.options.selectedIndex].value)
			{
				if (rankDropDown[i].name != thisRank.name)
				{ 
					if (rankDropDown[i].options[rankDropDown[i].options.selectedIndex].value < origRank && rankDropDown[i].options[rankDropDown[i].options.selectedIndex].value >= thisRank.options[thisRank.options.selectedIndex].value)
					{
						rankDropDown[i].options.selectedIndex = rankDropDown[i].options.selectedIndex + 1;
						rankDropDown[i].setAttribute('onchange','changeRank(\'' + rankDropDown[i].name + '\',' + rankDropDown[i].options[rankDropDown[i].options.selectedIndex].value + ');');
						alert(rankDropDown[i].outerHTML);
					}
				}
			}
			else
			{
			
				if (rankDropDown[i].name != thisRank.name && rankDropDown[i].options[rankDropDown[i].options.selectedIndex].value >= origRank)
				{
					if (thisRank.options[thisRank.options.selectedIndex].value >= rankDropDown[i].options[rankDropDown[i].options.selectedIndex].value)
					{
						rankDropDown[i].options.selectedIndex = rankDropDown[i].options.selectedIndex - 1;
						rankDropDown[i].setAttribute('onchange','changeRank(\'' + rankDropDown[i].name + '\',' + rankDropDown[i].options[rankDropDown[i].options.selectedIndex].value + ');');
						alert(rankDropDown[i].outerHTML);
					}
				}
			}
		}
		thisRank.onchange = '"changeRank(\'' + changedID + '\',' + thisRank.options[thisRank.options.selectedIndex].value + ');"';
		alert(thisRank.outerHTML);
	}

Now the alert boxes show, "so far" then the HTML for each select box that changes for thew outerHTML alerts. The HTML looks fine; displays like this:
Code:
<select name="rank#" id="rank#" onchange="changeRank('rank#', 1);"><option value="1" selected>1</option>....
where the # is the ID from the DB (MS ACCESS) and the 1 is the NEW Rank. But it refuses to handle the event again. I tried both setting it with the setAttribute('onchange',.... and just flat out thisRank.onchange = 'changeRank'... as you can see, but it won't run on the second instance of the event call. It WILL run on the second change if I don't change the 'onchange' value, but it messes up because the parameter for origRank is still coming from the original DB value i.e. I change the rank1 from one to six, everything looks ok, but origRank for rank1 is still 1 and if I change it again, it messes everything up. I know I can do a Jscript submit after the onchange event, but the form submits very very slow cause the DB is Access and has to update many records, so that is not an option. I also tried putting this.option[this.selectedIndex].value inside the onchange call, but it updates before the rest, and i need the origRank that was there before the last change, unless I rethink my whole logic, which I don't feel like doing right now.

Anyone know how I can change the onchange value in the select boxes whose rank have been changed and have it still handle the event?


Thanks,

Bob
 
Why change the function at all? Why not remove the second parameter, and have a global variable called "origRank" which you can then update in the function:

Code:
var origRank = 1;  // or whatever default value you want

function changeRank(changedID) {
   origRank = 2; // or whatever new value you want
...
}

Hope this helps,
Dan


[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks, but if I could assign it like:
Code:
origRank = thisRank.options[thisRank.options.selectedIndex].value
after making it a global var that would give me the NEW rank picked. I'm basing my code off of generating the other ranks from the original rank before the change and the new rank. Also, there are about 100+ dynamic select boxes to call from, so I'll need something to hold them all. I guess I could make an array to hold each origRank and update it based on the var i in the for loop. That sounds possible. I'm not near my dev pc so I can't really test it. Thanks. I needed a push in the right direction, and taking the origRank out of the onchange event should do the trick.


Bob


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top