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!

trouble changeing button element name 1

Status
Not open for further replies.

KABUSA

Technical User
Mar 9, 2004
17
US
Hi,
***
URL:Also have various test pages sleeves3,sleeves4 and sleeves5
***
Having trouble with windows XP running on PC with some but not all users.
I use a shopping cart program that needs to read a quantity selector and button for order processing. The cart needs to know the location of the select element on the form.
I use a simple script to step through the form and number each form element.
function elnumF()
{
//assign matching form element number to each element name
var Estop = document.forms[0].elements.length;
for(var x=0; x < Estop; x++)
{
document.forms[0].elements[x].name = x;
}
}
Once this is done, the buy function determines the select element location with the buynowF function.
It is called buy the button with this code :
Onclick ="buynowF(this,17900,'TECHNICS SL-BD20',1500)";

function buynowF(element,price,part,wt)
{
var element_num = parseInt(element.name);
var qty = element_num -1;
if (part.substring(0,1) == "*") parent.frames[0].adsF();
parent.frames[0].compute(0,qty,price,part,wt);
}

The trouble is that the person who is testing this for me has confirmed that:
1. if the buttons start out having the same name (set by the HTML NAME ="ADD") then the function elnumF above changes every element.name to 39 which on this page happens to be the number of total form elements.

2. If I leave the HTML NAME= off of the input tag then ElnumF has no effect. (I thought JS would create the element if it were absent from the HTML?)
The last test I have tried, but have yet to get a report back is to set the HTLM NAME="".This way the element would exist, but have no name.

I use windows 2000 pro and test on ie6, NS4.6, opera and mozilla and do not have a problem with this code.
Can anyone provide advice on what's going on.

I assume this is a quirk with the "this" keyword.
I assume that when all the buttons have the same name, that this new element array: forms.ADD[] is overiding the form.elements[] array for this one platform. But I do not know why?
is there some other way to get the element array position using the "this" keyword?

Thankyou Kevin
 
AFAIK not directly, but you can iterate through elements.collection and compare object references like variables:
Code:
<script language="javascript">
function blah( obj )
{	oFrm = document.forms[0];
	for(i=0; i<oFrm.elements.length && oFrm.elements[i]!= obj; i++);
	
	alert(i);
}
</script>
This means that elnumF() (dirrrty...) is not necessary.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top