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

Onchange and select()-problem

Status
Not open for further replies.

Quasibobo

Programmer
Oct 11, 2001
168
0
0
NL
Hi,

I've got these form fields that i need to check wiether the value of this field is less than zero. If so, give an alert message and select this field.

Code:
<form name="invoeren">
<input type ="bestellen_1_1" onChange="Optellen(1,4)">
<input type ="bestellen_1_2" onChange="Optellen(1,4)">
<input type ="bestellen_1_3" onChange="Optellen(1,4)">
<input type ="bestellen_1_4" onChange="Optellen(1,4)">
<input type ="bestellen_2_1" onChange="Optellen(2,3)">
<input type ="bestellen_2_2" onChange="Optellen(2,3)">
<input type ="bestellen_2_3" onChange="Optellen(2,3)">

and the function:
Code:
function Optellen(nummer,eind) {

som = 0;
for(i=1;i<eind;i++)
	{
	a = parseFloat(document.forms['invoeren' ].elements['bestellen_' + nummer+'_'+i].value);
	if(document.forms['invoeren' ].elements['bestellen_' + nummer+'_'+i].value.length==0)
		{
		a=0;
		}
	if(document.forms['invoeren' ].elements['bestellen_' + nummer+'_'+i].value<0)
		{
		a=0;
		document.forms['invoeren' ].elements['bestellen_' + nummer+'_'+i].value=0;
		alert('The value can\'t be less than zero!');
		document.forms['invoeren' ].elements['bestellen_' + nummer+'_'+i].select();
		}
	som += a;
	}

this works perfect exept for the document.forms['invoeren' ].elements['bestellen_' + nummer+'_'+i].select();
After perssing the TAB-button, de alertmessage appears, the value is set to zero but the focus is on the next textfield instead of back to the invalid textfield.

Can I overcome this? I saw a simular problem here a solution was to execute de select() with a setTimout.

But how do I do this with my funtions? Where do I set the quotes??

Code:
setTimeout("document.forms['invoeren' ].elements['bestellen_' + nummer+'_'+i].select()",1);
does NOT work....

Ynse
 
Try using something like this:

Code:
setTimeout('selectElement(' + nummer + ', ' + i + ');', 1);

and then add a function:

Code:
function selectElement(num1, num2) {
	var obj = document.forms['invoeren'].elements['bestellen_' + num1 + '_' + num2];
	obj.focus();
	obj.select();
}

Hope this helps,
Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Thanks.... a great idea! It works perfectly!

I sould remember this way of solving such a problem: just create another function....

Don't eat yellow snow!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top