Prady Prady - 1 year ago 70
jQuery Question

how can a call a function when text value of my textbox changes using jquery?

I have 3 input fields which have values prepopulated and a constant value of total hours 200. I am calculating the avghours as totalhours/datediff of date1 and date2.

My Question is if i change the value of date2 then i want the avghours value to change accordingly. I am not sure which event should be used to fire the method which does the calculation

<input type="text" id ="avghours"/>
<input type="text" id ="date1"/>
<input type="text" id ="date2"/>

Javascript code

function getavg()

$('#avghours').val()=totalhours/datediff($('#date2').val(),$('#date1').val(),'day');//datediff is userdefined function to get the datedifference


change will fire only if the focus is lost so that might not be possible is there anyother event that i can use.



Answer Source




 $("#date2").bind("keyup blur change mouseup",function () {
     var d = parseInt(datediff($('#date2').val());
     var v = (totalhours/d) + ' ' + $('#date1').val() + ' day'; <-- mergestrings

assign via the function instead of the right hand.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download