0

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.

Thanks

Prady

5 Answers 5

2

change

$('#avghours').val()=totalhours/datediff($('#date2').val(),
                                             $('#date1').val(),'day');

to

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

assign via the function instead of the right hand.

Sign up to request clarification or add additional context in comments.

5 Comments

i couldnt understand why the mergestring? datediff was a userdefined function to calulate the date difference. Or am i missing something very obvious?
no, I understand your correct code, however, I prefer to keep it a bit readable ;)
oh ok... i got it now... but would keyup work if keyboard is not used. i mean like its pasted using mouse
edited that a bit, added a bunch of events you can select according your requirements.
oh tats cool.. didnt know you could add bunch of events like this :)
0

If you want to update it immediately when the user types you could use the keyup event.

1 Comment

what happens if the user doesnot use the keyboard he pastes the date from another field using mouse would this event fire?
0

you could try keypress so...

$('#date2').keypress(function() {
//do calculating average logic here
});

then this would pick up when the value of the textbox has been changed

Comments

0

You can use the .change() event for this.

$("#date2").change(function(){

});

1 Comment

change event fires only if the focus is lost
0
    [![<html>
<head>
<title></title>
<script>

function multy(val)
{
alert(val.value+"--"+val.id);
}

</script>
</head>
<body>
<input id="txtdemo" type="text" onchange="multy(this);"></br>
<input id="txtdemo" type="text" onchange="multy(this);">
</input>
</body>
</html>][1]][1]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.