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!

odd element.name changing problem in IE

Status
Not open for further replies.

genis

Programmer
Aug 15, 2002
6
US
Hi all.
Upon deciding current browsers CSS implementation didn't allow select boxes to look as spiffy as i wanted... i wrote this little script which acts like a select box (sort of) but isn't.
Anywho it works Grreeaat in Mozilla.
IE always gives me heaps of trouble, because it hates me and tries to kill me daily.

If ya'll can figure out why this is giving me problems I'd appreciate any help. Thanks.
Here's a link to it to see it live -->
and here's dee code, the part that counts anyway :

Code:
<script type=&quot;text/javascript&quot;>
function altSelect(nAme,iD,num) {
	var meType = &quot;type&quot; + num;
	var select = &quot;select&quot; + num;
	var defBg = &quot;#FFF&quot;;
	var selectBg = &quot;#0A246A&quot;;
	if(nAme == meType)
		return;
	optionArr = document.getElementsByName(meType);
	if(optionArr.length != 0) {
		option = optionArr.item(0);
		option.name = select;
	}
	optionArr = document.getElementsByName(nAme);
	for(var i=0;i<optionArr.length;i++) {
		option = optionArr.item(i);
		option.style.backgroundColor = defBg;
		option.style.color = &quot;#000&quot;;
	}
	option = document.getElementById(iD);
	option.name = meType;
	option.style.backgroundColor = selectBg;
	option.style.color = &quot;#FFF&quot;;
}
function chgName(poop) {
	blah = document.getElementById(poop);
	blah.name = &quot;select1&quot;;
}
	</script>
</head>
<body>
		<form action=&quot;vardump.php&quot; method=&quot;get&quot;>
		<div id=&quot;select&quot;>
			<input name=&quot;select1&quot; id=&quot;gold1&quot; type=&quot;text&quot; readonly=&quot;readonly&quot; value=&quot;Gold&quot; onfocus=&quot;altSelect(this.name,this.id,1);&quot; />
			<input name=&quot;select1&quot; id=&quot;silv1&quot; type=&quot;text&quot; readonly=&quot;readonly&quot; value=&quot;Silver&quot; onfocus=&quot;altSelect(this.name,this.id,1);&quot; />
			<input name=&quot;select1&quot; id=&quot;plat1&quot; type=&quot;text&quot; readonly=&quot;readonly&quot; value=&quot;Platinum&quot; onfocus=&quot;altSelect(this.name,this.id,1);&quot; />
			<input name=&quot;select1&quot; id=&quot;pall1&quot; type=&quot;text&quot; readonly=&quot;readonly&quot; value=&quot;Palladium&quot; onfocus=&quot;altSelect(this.name,this.id,1);&quot; />
		</div>
		<input type=&quot;submit&quot; value=&quot;submit&quot; />
		</form>

hope that worked... Thanks.
 
You must be a VB programmer. :)# In Javascript, you need to use square brackets for array elements:

option = optionArr.item(0);

and

option = optionArr.item(i);

should be:

option = optionArr.item[0];

and

option = optionArr.item;

 
You must be a VB programmer. :)# In Javascript, you need to use square brackets for array elements:

option = optionArr.item(0);

and

option = optionArr.item(i);

should be:

option = optionArr.item[0];

and

option = optionArr.item;

 
nah, item() is a sort of function.

optionArr.item(0)
is just a longhand way of saying

optionArr[0]

why do i do it that way?
brackets scare me.

Any other help? no one can seem to solve this on any forum.
It's all IE's fault... it's da DEBBIL!!
 
Okay I've figured it out... and it is IE's fault as far as I can tell.

it seems getElementsByName() is the problem.
IE seems to return the the name that is hardcoded into the page always.
Even if a javascript function has changed &quot;select1&quot; to &quot;test1&quot; IE will return &quot;select1&quot;... because that's what it sees is hardcoded into the page.

Thus in the first if statement where I'm searching for elements by the name of &quot;type1&quot; using
getElementsByName()
none are found because that's not what is hardcoded.

BUT, it still retains that name in the javascript realm... so upon clicking again (this.name) == &quot;type1&quot; so the function returns.

Mozilla does everything correctly.

Did I mention i hate IE and it keeps trying to kill me?
well i do....
and it does.

Hope you all enjoyed this lesson with me... i know i didn't.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top