Applying JS Function And Event Listener To Multiple Html Elements
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 work
Solution 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.
Solution 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.
Solution 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>
Solution 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');
}
Post a Comment for "Applying JS Function And Event Listener To Multiple Html Elements"