Script crashing IE

L

Luis

When I run the following script it displays a form with a dropdown list
and four text boxes. If I select "No" from the dropdown list all the
text boxes are disabled. If I select "Yes" from the dropdown list
Internet Explorer 6 stops responding.

What I want it to do is:
- Disable all the text boxes if "No" is selected,
- Determine which text field has the focus and then select the text that
is inside the text field if "Yes" is selected

Why is it crashing IE?

<html>
<head>
<script language="JavaScript">
function IsFieldActive(fld) {
if ((document.frmFormName.OptionsList.value == "No")) {
DisableFields(fld);
}
else {
SelectFields(fld)
}
}

function DisableFields(fld) {
this.frmFormName.field1.blur(fld)
this.frmFormName.field2.blur(fld)
this.frmFormName.field3.blur(fld)
this.frmFormName.field4.blur(fld)
}

function SelectFields(fld) {
this.frmFormName.field1.focus(fld)
this.frmFormName.field1.select(fld)

this.frmFormName.field2.select(fld)
this.frmFormName.field2.select(fld)

this.frmFormName.field3.select(fld)
this.frmFormName.field3.select(fld)

this.frmFormName.field4.select(fld)
this.frmFormName.field4.select(fld)
}
</script>
</head>
<body>
<form name="frmFormName">
<table border="1" align="center">
<tr>
<td>Make Fields Active</td>
<td colspan="4">
<select name="OptionsList" size="1">
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</td>
</tr>
<tr>
<td>Field 1:</td>
<td><input type="text" name="field1" value="1"
onFocus="IsFieldActive(this.form.field1.value)"></td>
<td>Field 2:</td>
<td><input type="text" name="field2" value="2"
onFocus="IsFieldActive(this.form.field2.value);"></td>
</tr>
<tr>
<td>Field 3:</td>
<td><input type="text" name="field3" value="3"
onFocus="IsFieldActive(this.form.field3.value);"></td>
<td>Field 4:</td>
<td><input type="text" name="field4" value="4"
onFocus="IsFieldActive(this.form.field4.value);"></td>
</tr>
</table>
</form>
</body>
</html>
 
G

Grant Wagner

Luis said:
When I run the following script it displays a form with a dropdown list
and four text boxes. If I select "No" from the dropdown list all the
text boxes are disabled. If I select "Yes" from the dropdown list
Internet Explorer 6 stops responding.

What I want it to do is:
- Disable all the text boxes if "No" is selected,
- Determine which text field has the focus and then select the text that
is inside the text field if "Yes" is selected

Why is it crashing IE?

Because you are calling the select() method on a form control after focus
has been set to that control. Calling select() sets focus to the control,
which triggers the onfocus event, which calls select() which sets focus,
which triggers the onfocus event, which calls select() etc etc.

As well, there are number of other errors, such as select() and blur() don't
take arguments, and you can't select() more than a single input - or rather,
calling select() on subsequent controls moves the focus and selects only
that control's value.

Given your requirements:
What I want it to do is:
- Disable all the text boxes if "No" is selected,
- Determine which text field has the focus and then select the text that is
inside the text field if "Yes" is selected

The solution is easy:

<script type="text/javascript">
function IsFieldActive(inp) {
var sel;
if (inp.form &&
(sel = inp.form.elements['OptionsList'])) {

if (sel.options[sel.selectedIndex].value == 'Yes') {
// Yes is selected, select
// the value of this input
inp.select();
} else {
// the only input you need to blur()
// is the one that just got focus
inp.blur();
}
}
}
</script>

and each of your inputs should read:

<input type="text" name="field1" onfocus="IsFieldActive(this);">
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,600
Members
45,180
Latest member
CryptoTax Software
Top