Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you listen for multiple field changes in javascript?

I currently have the following code to update a URL. How do I combine this into something more robust so that I'm not repeating myself 3 times basically. I only want this to update when one of the fields is changed.

$(document).ready(function(){
  $('#device_select').change(function() {
      var device = $(this).val();
      var face = $('#face_select').val();
      var position = $('#position_select').val();
      document.getElementById("add_dev_rack").href="127.0.0.1:8080/dcim/devices/"+device+"/edit/?face="+face+"&position="+position;
  });
  $('#face_select').change(function() {
      var device = $('#device_select').val();
      var face = $(this).val();
      var position = $('#position_select').val();
      document.getElementById("add_dev_rack").href="127.0.0.1:8080/dcim/devices/"+device+"/edit/?face="+face+"&position="+position;
  });
  $('#position_select').change(function() {
      var device = $('#device_select').val();
      var face = $('#face_select').val();
      var position = $(this).val();
      document.getElementById("add_dev_rack").href="127.0.0.1:8080/dcim/devices/"+device+"/edit/?face="+face+"&position="+position;
  });
}); 
like image 828
csling Avatar asked Apr 20 '26 19:04

csling


1 Answers

$('#position_select, #device_select, #face_select').change(function() {
      var device = $('#device_select').val();
      var face = $('#face_select').val();
      var position = $('#position_select').val();
      document.getElementById("add_dev_rack").href="127.0.0.1:8080/dcim/devices/"+device+"/edit/?face="+face+"&position="+position;
  });

Have you tried somethign like this?

like image 80
Matej Žvan Avatar answered Apr 23 '26 08:04

Matej Žvan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!