You are not logged in.

#1 2009-10-26 15:56:07

schivmeister
Developer/TU
From: Singapore
Registered: 2007-05-17
Posts: 971
Website

[html][javascript] Get Result from Function and Output

<html>
  <head>
    <title>StupidEvaluation</title>
    <script type="text/javascript">
      <!--
      var myresult = "";

      function EvalInput(input) {
        var result = eval(input);
        myresult = result;
        alert(result);
      }
      // -->
    </script>
  </head>
  <body>
    <center>
      <form name="calcform">

        <input name="calcbox" type=text><br>

        <!-- Run function -->
        <input name="calcbtn" type=button value="Calculate!"
         onclick="EvalInput(calcform.calcbox.value);">

        <input name="calcdel" type=reset value="Erase"><br>

        <!-- Get result from function somehow and display somewhere -->
        <script type="text/javascript">
          document.write(myresult);
        </script>

      </form>
    </center>
  </body>
</html>
<!-- vim:set ts=2 sw=2 et: -->

I'm facing two problems with this:

1) I can't seem to figure out how one would actually print a result produced by a function from an onclick event, instead of using some popup function like alert().

2) It appears impossible to get the Return/Enter key to act like a click. I've tried both submit and manually matching the keycode but these actually clear the input box after the function has finished running - I don't want that.


I need real, proper pen and paper for this.

Offline

#2 2009-10-26 19:52:54

litemotiv
Forum Fellow
Registered: 2008-08-01
Posts: 5,026

Re: [html][javascript] Get Result from Function and Output

try something like this, it should cover both questions:

<html>

<input type="text" id="myText">
<input type="button" id="myButton" value="Pressme!">

<div id="status"></div>

<script type="text/javascript">

myButton.onkeypress = function(e) {
    myText.value = e.keyCode;
    document.getElementById('status').innerHTML = 'Succes!';
}
</script>
</html>

ᶘ ᵒᴥᵒᶅ

Offline

#3 2009-10-27 22:55:36

N30N
Member
Registered: 2007-04-08
Posts: 273

Re: [html][javascript] Get Result from Function and Output

schivmeister wrote:

1) I can't seem to figure out how one would actually print a result produced by a function from an onclick event, instead of using some popup function like alert().

You should use the DOM.

schivmeister wrote:

2) It appears impossible to get the Return/Enter key to act like a click. I've tried both submit and manually matching the keycode but these actually clear the input box after the function has finished running - I don't want that.

Your "Calculate!" button should be of type="submit" and then preform the calculation on the form's "submit" event.

Offline

#4 2009-11-05 13:05:55

schivmeister
Developer/TU
From: Singapore
Registered: 2007-05-17
Posts: 971
Website

Re: [html][javascript] Get Result from Function and Output

Hey thanks guys..I've gone on to do it all from scratch, I'm trying to see what's possible and maybe I'll dive into MathML =p

N30N: That's exactly what I tried and didn't like, because it cleared the input upon submit.


I need real, proper pen and paper for this.

Offline

#5 2009-11-05 19:20:33

N30N
Member
Registered: 2007-04-08
Posts: 273

Re: [html][javascript] Get Result from Function and Output

schivmeister wrote:

N30N: That's exactly what I tried and didn't like, because it cleared the input upon submit.

Call the preventDefault event method (and also set the returnValue property to false if your supporting IE's non standardness) to stop that.

Offline

#6 2009-11-05 19:29:09

schivmeister
Developer/TU
From: Singapore
Registered: 2007-05-17
Posts: 971
Website

Re: [html][javascript] Get Result from Function and Output

You are one awesome archer wink


I need real, proper pen and paper for this.

Offline

Board footer

Powered by FluxBB