1 Replies - 223 Views - Last Post: 24 September 2013 - 04:03 AM

#1 DkSnowdon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 137
  • Joined: 31-October 12

event on change question

Posted 24 September 2013 - 03:30 AM

Does the event on change only run the scripts if the user changes something it will it run if another function changes somthing.


for example the first function runs when the user inputs something and then i want ot second function to run when the first one changes the value
/* Update calculation for collumn 5 */
	$("input[name='row2[]'][type=text]").eq(4).on("change", function ()
	{
		var value1 = getCellValue(2,2); // get the value for length
		var value2 = getCellValue(2,3); // get the value for height
		var value3 = this.value; // get the value for the width
		
		/* Update dependents */
		$("input[name='row2[]'][type=text]").eq(6).val(skipsCalc(value1, value2, value3)); // proform the calc and update the skips cell for row 2 
		$("input[name='row2[]'][type=text]").eq(5).val(m3Calc(value1, value2, value3)); // proform the calc and update the m3 cell for row 2 
			
	}) // End on change row2 collumn5
	
	
	/* Update calculation for collumn 6 */
	$("input[name='row2[]'][type=text]").eq(6).on("change", function ()
	{
		alert("hhhh");
		// Update materials
		$("input[name='row2[]'][type=text]").eq(9).val(ExcavateFoundMaterialCalc(this.value));
		
	 	// Update labour
			
	}) // End on change row2 collumn6


Is This A Good Question/Topic? 0
  • +

Replies To: event on change question

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: event on change question

Posted 24 September 2013 - 04:03 AM

Quote

for example the first function runs when the user inputs something and then i want ot second function to run when the first one changes the value

you cannot resolve that with the change handler, as this would create an infinite loop (the calculations would cause a change event, which would trigger recalculation, etc.).

a typical use case for Promises. (more or less exact implemented by jQuery.Deferred).

just based upon a quick jQuery look-up:
$('input').on("change", function() {
    $.Deferred()
      .then(function(value) {
        return calculation1(value);
      })
      .then(function(value) {
        return calculation2(value);
      })
      .then(function(value) {
        $('#output').val(value);
      }, function() {
        $('#error').text('calculation failed');
      }
      .resolve(this.value)
    ;
});


This post has been edited by Dormilich: 24 September 2013 - 04:09 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1