You are viewing limited content. For full access, please sign in.

Question

Question

Javascript to monitor for changes on $(this)

asked on August 3, 2017 Show version history

I have a scenario where I would like to copy the value from the first field selected with the class name "load". Then I want to place a monitor on that specific field for changes. I thought I had it all figured out by simply adding a class to the field called "monitor" then have a function that runs on change. Everything works, except the function that runs on change of the "monitor" class.

 

//Wait for page to load
$(document).ready(function () {

 
//Set start time from first load selected
  $('.load select').on('change', function(event) {

    //Get selected value
    var res = $(this).val();
    
    //Get current value of output field
    var current = $('.start input').val();

    //If output field is blank
    if (current.length == 0) {
    
      //$(this).classList.add('monitor'); Doesn't work for some reason
      //Add new class to selected field for monitoring changes
      document.getElementById(event.target.id).classList.add('monitor');
      
      //Set value of output field
      $('.start input').val(res + "m");
        
    }

  });
  
  
//Monitor for changes on first selected field and update output field
  $('.monitor select').on('change', function() {
    
    var res = $(this).val();
    
      $('.start input').val(res + "m");

  }); 

}); //End of on form load

 

 

0 0

Answer

SELECTED ANSWER
replied on August 3, 2017

Yes you can can alter your watch function to watch a parent element such as the body tag and add in an extra filter to listen for when a '.monitor' element value changes

$('body').on('change', '.monitor', function () {...});
0 0

Replies

replied on August 3, 2017 Show version history

The reason this doesn't work is because your watching function only listens to '.monitor select' elements that exist at the time of initial rendering. 

0 0
replied on August 3, 2017

Is there any way to access something that doesn't exist until after the initial load?

0 0
SELECTED ANSWER
replied on August 3, 2017

Yes you can can alter your watch function to watch a parent element such as the body tag and add in an extra filter to listen for when a '.monitor' element value changes

$('body').on('change', '.monitor', function () {...});
0 0
replied on August 7, 2017

perfect, that is what I needed. Thank you!

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.