onfocus causes infinite loop RRS feed

  • Question

  • User1310055179 posted


    I have a web form application that automatically creates forms according to a text file.

    The "CreateDynamicForm" function is called from CreateChildControls and after each postback.

    All fields are automatically posting back to the server.

    When I add a dropdownlist field, I am also adding onfocus function from code behind:

    nDropDownList.Attributes.Add("onfocus""nm_dropdownlist_focus('" + i + "');");

    For some reason, this causes an infinite loop and the js function seems to be fired again and again:

    function nm_dropdownlist_focus(index)
                var add = document.getElementById("nAdd_" + index);
                var edit = document.getElementById("nEdit_" + index);
                add.style.display = 'inline';
                add.style.position = 'relative';
                add.style.top = '0mm';
                add.style.left = '0mm';
                add.style.zIndex = '5000';
                edit.style.display = 'inline';
                edit.style.position = 'relative';
                edit.style.top = '0mm';
                edit.style.left = '0mm';
                edit.style.zIndex = '5000';

    I have no idea why this is suddenly happening. I remember it used to work just fine.

    This is the js error I get:

    Uncaught TypeError: Cannot read property 'style' of null
    at nm_dropdownlist_focus

    I have noticed it happens when the dropdownlist has no items.

    Any idea why do I get this error and how to get rid of it?


    Tuesday, June 18, 2019 12:40 PM

All replies

  • User475983607 posted

    The error does not indicate an infinite loop.  The error indicates the code is trying to access the style property of a null object.


    Any idea why do I get this error and how to get rid of it?

    I assume the a selector...

    var add = document.getElementById("nAdd_" + index);

    returns undefined.  Try setting a breakpoint in the script and debugging the code.  Also view the HTML using Dev tools (F12) and make sure the element Id exists in the markup.

    Tuesday, June 18, 2019 1:38 PM
  • User288213138 posted

    Hi qsoft_developer,

    Based on the code you provided, I cannot reproduce your problem.

    But "Uncaught TypeError: Cannot read property 'style' of null" error may be caused by 

    var add = document.getElementById("nAdd_" + index);
    var edit = document.getElementById("nEdit_" + index);

    May be there is no id item in your code,this will result in not being able to be successfully retrieved.

    So please debug you code and use F12 to check if the element id exists.

    If the problem has not been solved, please post your code.

    Best regards,


    Thursday, June 20, 2019 10:48 AM