Script crashing IE

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Luis

    Script crashing IE

    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="JavaS cript">
    function IsFieldActive(f ld) {
    if ((document.frmF ormName.Options List.value == "No")) {
    DisableFields(f ld);
    }
    else {
    SelectFields(fl d)
    }
    }

    function DisableFields(f ld) {
    this.frmFormNam e.field1.blur(f ld)
    this.frmFormNam e.field2.blur(f ld)
    this.frmFormNam e.field3.blur(f ld)
    this.frmFormNam e.field4.blur(f ld)
    }

    function SelectFields(fl d) {
    this.frmFormNam e.field1.focus( fld)
    this.frmFormNam e.field1.select (fld)

    this.frmFormNam e.field2.select (fld)
    this.frmFormNam e.field2.select (fld)

    this.frmFormNam e.field3.select (fld)
    this.frmFormNam e.field3.select (fld)

    this.frmFormNam e.field4.select (fld)
    this.frmFormNam e.field4.select (fld)
    }
    </script>
    </head>
    <body>
    <form name="frmFormNa me">
    <table border="1" align="center">
    <tr>
    <td>Make Fields Active</td>
    <td colspan="4">
    <select name="OptionsLi st" 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="IsFiel dActive(this.fo rm.field1.value )"></td>
    <td>Field 2:</td>
    <td><input type="text" name="field2" value="2"
    onFocus="IsFiel dActive(this.fo rm.field2.value );"></td>
    </tr>
    <tr>
    <td>Field 3:</td>
    <td><input type="text" name="field3" value="3"
    onFocus="IsFiel dActive(this.fo rm.field3.value );"></td>
    <td>Field 4:</td>
    <td><input type="text" name="field4" value="4"
    onFocus="IsFiel dActive(this.fo rm.field4.value );"></td>
    </tr>
    </table>
    </form>
    </body>
    </html>


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • Grant Wagner

    #2
    Re: Script crashing IE

    Luis wrote:
    [color=blue]
    > 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?[/color]

    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(i np) {
    var sel;
    if (inp.form &&
    (sel = inp.form.elemen ts['OptionsList'])) {

    if (sel.options[sel.selectedInd ex].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="IsFiel dActive(this);" >

    --
    Grant Wagner <gwagner@agrico reunited.com>
    comp.lang.javas cript FAQ - http://jibbering.com/faq

    Comment

    Working...