How do I get the previously focused element in JavaScript?

前端 未结 7 1725
死守一世寂寞
死守一世寂寞 2021-01-04 03:46

I have an HTML form with multiple text inputs. I want to clear the element that had the focus immediately prior to when the \'Clear\' button is pressed. How do I ge

7条回答
  •  执笔经年
    2021-01-04 04:24

    Create a global variable for storing the current focused element's id,

    var cur_id;
    

    call one function for onblur of each of elements and pass id

    
    

    and write the set the id to global variable from that function

    function setId(id) {
        cur_id = id;
    }
    

    and write a function for onclick of clear button, like this

    function clear() {
        document.getElementById(cur_id).value = "";
    }
    

提交回复
热议问题