Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-02-2007, 02:07 PM   PM User | #1
NancyJ
Senior Coder

 
NancyJ's Avatar
 
Join Date: Feb 2005
Location: Bradford, UK
Posts: 3,162
Thanks: 19
Thanked 65 Times in 64 Posts
NancyJ will become famous soon enough
dropdown sets

Some time ago someone gave me a script to do this:

A group of select inputs with identical options
When an option is selected from 1 dropdown it is removed from the others (unless its a blank one)

Now I'm reusing the script in another application that has multiple sets of dropdowns (but only 1 that the script needs to apply to)

It works fine except, all the options get jumbled up - in all the dropdowns.

The script is a little beyond my level of javascript - I think largely because it doesn't use 'sensible' variable names so its quite hard to follow. How can I modify the script to not mess up the order (most importantly of the other dropdowns that aren't having elements removed and added)

Code:
		    <script type='text/javascript'><!--//--><![CDATA[//><!--

var onoff = 1;


function SelInit(sobj,srt)
{	if (typeof(sobj)=='string'){ sobj=document.getElementById(sobj); }
	srt=srt||0;
	var p=sobj.parentNode.parentNode.parentNode;
	p.ary=[];
	var sels=p.getElementsByTagName('SELECT');
	for (A0=0;A0<sels.length;A0++)
	{	sels[A0].ary=p.ary;
		p.ary.push(sels[A0]);
		sels[A0].last=null;
		sels[A0].srt=srt;
	}
	return p;
} // end of js fn SelInit



function RemoveDuplicates(obj,srt)
{	if (onoff)
	{	if (typeof(obj)=='string'){ obj=document.getElementById(obj); }

		if (!obj.ary){ SelInit(obj,srt); }
	
		if (obj.replace)
		{	for (B0=0;B0<obj.replace.length;B0++)
			{	obj.replace[B0][0].appendChild(obj.replace[B0][1]);
			}
		}
	
		obj.last=obj.options[obj.selectedIndex];
		for (B1=0;B1<obj.ary.length;B1++)
		{	obj.ary[B1].si=obj.ary[B1].options[obj.ary[B1].selectedIndex].value;
			if (obj.ary[B1]!=obj)
			{	SelOpts(obj.ary[B1],srt);
				obj.ary[B1].opts.sort(SortOptions);
			}
		}
	
		obj.replace=[];
		for (B2=0;B2<obj.ary.length;B2++)
		{	if (obj.ary[B2]!=obj)
			{	for (var B3=0;B3<obj.ary[B2].opts.length;B3++)
				{	if (obj.ary[B2].opts[B3][1]!=obj.si||obj.options[obj.selectedIndex].value.match('-1'))
					{	obj.ary[B2].appendChild(obj.ary[B2].opts[B3][0]);
					} else
					{	obj.replace.push([obj.ary[B2],obj.ary[B2].opts[B3][0].cloneNode(true)]);
					}
				}
			}
		}
	
		for (B3=0;B3<obj.ary.length;B3++)
		{	obj.ary[B3].selectedIndex=0;
			for (B4=obj.ary[B3].srt;B4<obj.ary[B3].options.length;B4++)
			{	if (obj.ary[B3].options[B4].value==obj.ary[B3].si)
				{	obj.ary[B3].selectedIndex=B4;
				}
			}
		}
	}
		
} // end of js fn RemoveDuplicates

function SelOpts(sobj,srt)
{	sobj.opts=[]; sobj.old=[];
	for (var C0=srt;C0<sobj.options.length;C0++)
	{	sobj.opts.push([sobj.options[C0].cloneNode(true),sobj.options[C0].value]);
		sobj.old.push(sobj.options[C0]);
	}
	for (var C1=0;C1<sobj.old.length;C1++)
	{	sobj.removeChild(sobj.old[C1])
	}
} // end of js fn SelOpts

function SortOptions(a,b)
{	var A=parseInt(a[1]); var B=parseInt(b[1]);
	if (A<B){ return -1; }
	if (A>B){ return +1; }
	return 0;
} // end of js fn SortOptions
//--><!]]></script>
if I set srt = 0 then the elements in the dropdowns that I'm trying to affect, stay in the same order except the blank element at the top - which moves down the list (usually somewhere between the bottom and the middle) . And the ones I want to stay the same get jumbled up.

If I set srt =1 then the blank one stays at the top but the others jumble about a bit (I'm not majorly bothered by this) but the other dropdowns still all get jumbled up
__________________
http://www.hazelryan.co.uk
NancyJ is offline   Reply With Quote
Old 09-02-2007, 02:38 PM   PM User | #2
NancyJ
Senior Coder

 
NancyJ's Avatar
 
Join Date: Feb 2005
Location: Bradford, UK
Posts: 3,162
Thanks: 19
Thanked 65 Times in 64 Posts
NancyJ will become famous soon enough
I also just noticed that if you select the black top option that all the blank options disappear
__________________
http://www.hazelryan.co.uk
NancyJ is offline   Reply With Quote
Old 09-02-2007, 02:52 PM   PM User | #3
NancyJ
Senior Coder

 
NancyJ's Avatar
 
Join Date: Feb 2005
Location: Bradford, UK
Posts: 3,162
Thanks: 19
Thanked 65 Times in 64 Posts
NancyJ will become famous soon enough
I've made some adjustments and I've fixed everything except the jumbled up elements in the ones I'm adding and removing from.

Code:
		    <script type='text/javascript'><!--//--><![CDATA[//><!--

var onoff = 1;


function SelInit(sobj,srt)
{	if (typeof(sobj)=='string'){ sobj=document.getElementById(sobj); }
	srt=srt||0;
	var p=document;
	p.ary=[];
	var sels=getElementsByClassName(document, 'select', 'parts');
	for (A0=0;A0<sels.length;A0++)
	{	sels[A0].ary=p.ary;
		p.ary.push(sels[A0]);
		sels[A0].last=null;
		sels[A0].srt=srt;
	}
	return p;
} // end of js fn SelInit


function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/-/g, "\-");
	var oRegExp = new RegExp("(^|\s)" + strClassName + "(\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

function RemoveDuplicates(obj,srt)
{	if (onoff)
	{	if (typeof(obj)=='string'){ obj=document.getElementById(obj); }

		if (!obj.ary){ SelInit(obj,srt); }
	
		if (obj.replace)
		{	for (B0=0;B0<obj.replace.length;B0++)
			{	obj.replace[B0][0].appendChild(obj.replace[B0][1]);
			}
		}
	
		obj.last=obj.options[obj.selectedIndex];
		for (B1=0;B1<obj.ary.length;B1++)
		{	obj.ary[B1].si=obj.ary[B1].options[obj.ary[B1].selectedIndex].value;
			if (obj.ary[B1]!=obj)
			{	SelOpts(obj.ary[B1],srt);
				obj.ary[B1].opts.sort(SortOptions);
			}
		}
	
		obj.replace=[];
		for (B2=0;B2<obj.ary.length;B2++)
		{	if (obj.ary[B2]!=obj)
			{	for (var B3=0;B3<obj.ary[B2].opts.length;B3++)
				{	if (obj.ary[B2].opts[B3][1]!=obj.si||obj.options[obj.selectedIndex].value.match('-1'))
					{	obj.ary[B2].appendChild(obj.ary[B2].opts[B3][0]);
					} else
					{	obj.replace.push([obj.ary[B2],obj.ary[B2].opts[B3][0].cloneNode(true)]);
					}
				}
			}
		}
	
		for (B3=0;B3<obj.ary.length;B3++)
		{	obj.ary[B3].selectedIndex=0;
			for (B4=obj.ary[B3].srt;B4<obj.ary[B3].options.length;B4++)
			{	if (obj.ary[B3].options[B4].value==obj.ary[B3].si)
				{	obj.ary[B3].selectedIndex=B4;
				}
			}
		}
	}
		
} // end of js fn RemoveDuplicates

function SelOpts(sobj,srt)
{	sobj.opts=[]; sobj.old=[];
	for (var C0=srt;C0<sobj.options.length;C0++)
	{	sobj.opts.push([sobj.options[C0].cloneNode(true),sobj.options[C0].value]);
		sobj.old.push(sobj.options[C0]);
	}
	for (var C1=0;C1<sobj.old.length;C1++)
	{	sobj.removeChild(sobj.old[C1])
	}
} // end of js fn SelOpts

function SortOptions(a,b)
{	var A=parseInt(a[1]); var B=parseInt(b[1]);
	if (A<B){ return -1; }
	if (A>B){ return +1; }
	return 0;
} // end of js fn SortOptions
//--><!]]></script>
Its not the end of the world really but I'd like to fix it if possible
__________________
http://www.hazelryan.co.uk
NancyJ is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:50 PM.


Advertisement
Log in to turn off these ads.