Applying JS function and event listener to multiple html elements

纵然是瞬间 提交于 2019-12-11 07:26:08

问题


I am creating a form which I simplified below to get straight to the point, the JS code is to control the placeholder; hiding on focus and reappear on blur, obviously this now works for the input with id('Name') only. How do I apply these functions to all inputs elements DRY without repeating the code for each input.

var x = document.getElementById("Name");
x.addEventListener("focusin", myFocusFunction);
x.addEventListener("focusout", myBlurFunction);

function myFocusFunction() {
    document.getElementById('Name').placeholder= '';  
}

function myBlurFunction() {
    document.getElementById('Name').placeholder= 'Name';  
}
<div id='card'>
    <input id='Name' type='text' placeholder='Name'/>
    <input id='Age' type='text' placeholder='Age'/>
    <input id='Sex' type='text' placeholder='Sex'/>
    <input id='Occupation' type='text' placeholder='Occupation'/>
</div>

回答1:


Array.from(document.getElementsByTagName("input")).forEach(input => {

  input.addEventListener("focusin", focus);
  input.addEventListener("focusout", blur.bind(input, input.placeholder));

});


function focus() {
  this.placeholder = '';
}

function blur(placeholder) {
  this.placeholder = placeholder;
}
<div id='card'>
  <input id='Name' type='text' placeholder='Name' />
  <input id='Age' type='text' placeholder='Age' />
  <input id='Sex' type='text' placeholder='Sex' />
  <input id='Occupation' type='text' placeholder='Occupation' />
</div>

Using the this keyword you can access the object which the event was triggered on.

Also you need to preserve the placeholder somehow, in the example above I used bound function but you can chose your own way.

You can also find an example how to specify names manually in revision 2 of this answer.




回答2:


try to do ike this

    <!DOCTYPE html>
    <html>
    <body>
    
    <div id='card'>
    <input id='Name' type='text' placeholder='Name' onfocusin="myFocusFunction(event)" onblur="myBlurFunction(event,'Name')" />
    <input id='Age' type='text' placeholder='Age' onfocusin="myFocusFunction(event)" onblur="myBlurFunction(event,'Age')" />
    <input id='Sex' type='text' placeholder='Sex' onfocusin="myFocusFunction(event)" onblur="myBlurFunction(event,'Sex')" />
    <input id='Occupation' type='text' placeholder='Occupation' onfocusin="myFocusFunction(event)" onblur="myBlurFunction(event,'Occupation')" />
    </div>
    
    <script>
    function myFocusFunction(event) {
    
        var element = event.target.getAttribute('id');
        document.getElementById(element).placeholder= '';  
    }
    
    function myBlurFunction(event,placeHolder) {        
        var element = event.target.getAttribute('id');           
        document.getElementById(element ).placeholder= placeHolder ;  
    }
    
    </script>
    
    </body>
    </html>

on blur event we need to pass placeholder string because on focus we set placeholder as " " so, on blur event we can not get placeholder by getAttribute method because it always give null.




回答3:


You could give the elements a class, and iterate

document.querySelectorAll('.placeholder').forEach(function(el) {
  el.addEventListener("focusin", myFocusFunction);
  el.addEventListener("focusout", myBlurFunction);
});

function myFocusFunction() {
  document.getElementById('Name').placeholder = '';
}

function myBlurFunction() {
  document.getElementById('Name').placeholder = 'Name';
}
<div id='card'>
  <input id='Name' class="placeholder" type='text' placeholder='Name' />
  <input id='Age'  class="placeholder" type='text' placeholder='Age' />
  <input id='Sex'  class="placeholder" type='text' placeholder='Sex' />
  <input id='Occupation' type='text' placeholder='Occupation' />
</div>



回答4:


You can set a common class for the inputs that need this function,for example, ‘add-tip’, and then:

var x = document.querySelectorAll(".add-tip");
for (var i=0;i< x.length;i++) {
    x[i].addEventListener("focusin", myFocusFunction);
    x[i].addEventListener("focusout", myBlurFunction);
}
function myFocusFunction(e) {
    e.target.setAttribute('placeholder','');  
}

function myBlurFunction(e) {
    e.target.setAttribute('placeholder','Name');  
}


来源:https://stackoverflow.com/questions/46218854/applying-js-function-and-event-listener-to-multiple-html-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!