0

Hi I have tried a search but could not find an answer to my problem.. Im not very good with Javascript or JQuery so please be gentle with me lol

I have a form with multipal fields and what im tryin to do is hide a button when one of the fields has been changed.

I understand this is a OnChange type event but not sure how to go about if one of multipal fields have been changed to then hide the element

Heres what I have so far

<script type="text/javascript">
var opt1 = document.getElementById('BorderRange');
var opt2 = document.getElementById('bs');
var opt3 = document.getElementById('tb');
var opt4 = document.getElementById('wf');



opt1.onchange = function() {
   document.getElementById('pf').style.display = 'none';
}
</script>

So this code works if one of the fields has been changed ("BorderRange") but is there a simple way of linking all vars to one onchange function?

Something like opt1.onchange && opt2.onchange && opt3.onchange = function() for example

many thanks

John Dohh
  • 37
  • 7
  • also: [jQuery same click event for multiple elements](https://stackoverflow.com/questions/1313373/jquery-same-click-event-for-multiple-elements). Or the more general [What is DOM Event delegation?](https://stackoverflow.com/questions/1687296/what-is-dom-event-delegation#:~:text=Event%20delegation%20is%20handling%20an,on%20elements%20within%20the%20container.) – pilchard Jul 29 '22 at 20:20
  • Via the link that someone kindly shared this solved my problem - Many thanks! `$('#invent1, #invent2, #invent3').change(function () { // Do magical things }); ` – John Dohh Jul 29 '22 at 20:28

1 Answers1

1

You can add the change event listener to the form. Event bubbling will cause this to be triggered when any of the inputs in the form are changed.

document.getElementById("yourformID").addEventListener("change", function() {
    document.getElementById('pf').style.display = 'none';
});
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Hi thank you for this, I will keep this idea for future ref, I have been given a link to another answer which told me exactly what i needed - thank you again! – John Dohh Jul 29 '22 at 20:26