Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder kosstr12's Avatar
    Join Date
    Apr 2007
    Location
    Woodstock, Georgia
    Posts
    428
    Thanks
    7
    Thanked 5 Times in 5 Posts

    Javascript calculator

    I've made numerous different kinds of javascript calucators, like:
    Absolute Value
    Exponent Calculator
    Slope Calculator

    And, I used prompts for them, but is it possible to get the same result by using input already on the page, if so please share.

    Thanks,
    kosstr12

  • #2
    New Coder
    Join Date
    Apr 2007
    Location
    Silicon Valley California
    Posts
    71
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sure it's possible, you can pretty much do anything with any element on the page. You need to be more specific in what you want the calculator to do.

  • #3
    Regular Coder kosstr12's Avatar
    Join Date
    Apr 2007
    Location
    Woodstock, Georgia
    Posts
    428
    Thanks
    7
    Thanked 5 Times in 5 Posts
    Ok, lemme show you...go to slopecalc.5u.com, the home page is what I want all the other calculator to look like also, if you go to the other pages you can see that they all work from prompts

  • #4
    New Coder
    Join Date
    Apr 2007
    Location
    Silicon Valley California
    Posts
    71
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay, well add HTML markup for the input fields, a button, and a results div. Give each element a unique ID. Attach an 'onclick' observer to the button element that calls a function that you've included. Here is a very simple example:
    Code:
    <html>
    <head>
    ...
    <script text="javascript/text"><!--
    var addListener = function(element, name, observer) {
        if (element.addEventListener) {
          element.addEventListener(name, observer, false);
        }
        else if (element.attachEvent) {
          element.attachEvent('on' + name, observer);
        }
    };
    
    var addition = function() {
      var v1 = document.getElementById("val1").value;
      var v2 = document.getElementById("val2").value;
      var r = document.getElementById("results");
      r.innerHTML = parseInt(v1) + parseInt(v2);
    }
    
    var onPageLoad = function() {
    var btn = document.getElementById("myButton");
    addListener(btn, 'click', addition);
    }
    --></script>
    ...
    </head>
    
    <body onload="onPageLoad();">
    ...
    <form action="javascript:void(null)">
    <input id="val1" type="text" value="" />
    <input id="val2" type="text" value="" />
    <input id="myButton" type="button" value="Add" />
    <div id="results"></div>
    </form>
    ...
    </body>
    </html>
    that should be enough for you to get started.

  • #5
    Regular Coder kosstr12's Avatar
    Join Date
    Apr 2007
    Location
    Woodstock, Georgia
    Posts
    428
    Thanks
    7
    Thanked 5 Times in 5 Posts
    Thanks so much!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •