Jump to content

Refreshing a result


Tusk
 Share

Recommended Posts

I have a result that I display in a <input readonly='readonly'> field. It was being driven by a button, But I want it to recalculate or refresh every time anything is changed in other parts of the page (it's value is derived form other sections of the page)I have tried using "onchange" and "onclick" events but they don't seem to work every time? Is there another or better way of achieving this?

Link to comment
Share on other sites

Maybe try onblur, as that will be called after someone has left the form field. If you want it to update as someone is typing, then onmousedown might be better.Onchange should work, though. If the above doesn't work, you might be best posting the script you are trying here so we can see if it's anything else.

Edited by mnkn
Link to comment
Share on other sites

Thanks Mnkn, Below is an extract of the code.I want the result at the bottom "the <input readonly='readonly'> field" to update automatically if either of the the input areas above are changed.many thanks<html> <head> <script type= "JavaScript"></SCRIPT><meta http-equiv='Content-type' content='text/html;charset=UTF-8'><title>Calc Numbers</title><script type='text/javascript'>function calc() {var t = document.getElementById("ta2").value;var numbers = new Array();numbers = t.split(/\s+/);var sum = 0;var num = '';if (numbers.length == 1) { num = '0'; }for (i = 0; i < numbers.length; i++) {sum = sum + eval(numbers); if (i == numbers.length - 1) { num = num + " and ";} else { if (i > 0) { num = num + ", "} } num = num + numbers; }result = + sum +"<"+"/p>";var resElement = document.getElementById("res");resElement.innerHTML = result;}function a_div_b(form) { a=parseInt(result); b=parseInt(form.b.value,10); c=a/b; d=(Math.floor(a/:)); e=b*d; f=a-e; if (f==0) { form.ans.value = b } else { form.ans.value = f }}</script></head> <body bgcolor="#FFFFFF" background="bckgrnd.gif" text="#000000" leftmargin="0" topmargin="0" class="titlestyle"> <div align="center"> <br> <font size="5" face="comic sans ms">1)<br> <textarea id='ta2' onclick="this.value=''" onchange='calc()'cols = 65 rows = 5 ></textarea> <div id='res'></div> </font></div> <CENTER><FORM name="formx" style="font-family: 'Comic Sans MS'; Comic Sans MS&amp; quot;; font-size: small; color: #0000FF"> <font size="5" face="comic sans ms">2)<br></font> <input type=text size=4 value=0 name="b" style="font-size: medium; font-weight: normal; font-family: 'Comic Sans MS';">    <br><span class="style3"> </span><br> <input type="button" value="calc" onClick="a_div_b(this.form)">     <input readonly='readonly' value=0 name="ans" size=9 style="font-family: "Comic Sans MS"; font-size: medium; color: #0000FF; background-color: #FFFF99">  </FORM></CENTER></html>

Link to comment
Share on other sites

What happens when you put your function in the onchange event? Do you get any errors?Couple other things:- No closing </body> tag- Styles should be put in a stylesheet, either embedded (between <style> tags in the <head>) or external.

Link to comment
Share on other sites

Hi ShadowMage,its not that I get errors It doesn't always update unless I use the calc button. But I am trying to get it to update without the need of a button. I think it needs a function in the script section to update onblur but am not sure how to do that?

Link to comment
Share on other sites

its not that I get errors It doesn't always update unless I use the calc button. But I am trying to get it to update without the need of a button. I think it needs a function in the script section to update onblur but am not sure how to do that?
Exactly the same way you did it with the button. You can just put the calc function in the onchange event of your inputs.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...