AddEventListener for multiple elements doesn't work with “focus” event

前端 未结 3 1888
孤城傲影
孤城傲影 2021-01-25 19:17

I\'m guessing I have a basic error in thinking but I just can\'t get around it.

I have a couple of text fields which I want to add an EventListener to. I put them all i

3条回答
  •  爱一瞬间的悲伤
    2021-01-25 20:12

    I think you have to use querySelectorAll() that will return all the inputs :

    var fields = document.querySelectorAll('#parent input');
    

    And use loop to attach focus event to every field :

    for (var i = 0; i < fields.length; i++) {
        fields[i].addEventListener('focus', emptyField, false);
    }
    

    Hope this helps.

提交回复
热议问题