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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    200
    Thanks
    27
    Thanked 2 Times in 2 Posts

    form update depending on which values were entered

    Hello,

    I have a form that works out some basic math questions e.g. area etc.

    I run a function when a form field is updated to do the calculations, however I'd like to run a different calculation depending on which form element was updated.

    Is there a way to say something like:

    if (area1 was updated) {
    work out this calculation
    }
    if(area2 was updated) {
    work out this calculation
    }

    and so on?

    Thanks,

    Nicola

  • #2
    Regular Coder
    Join Date
    Jan 2006
    Posts
    568
    Thanks
    6
    Thanked 84 Times in 84 Posts
    Code:
    var inputs=Array.prototype.slice.call(document.getElementsByTagName("input"));
    
    for(i in inputs) {
    inputs[i].addEventListener("keyup", getUpdate, false); //not sure about "keyup" here
    }
    
    function getUpdate() {
    //'this' keyword=target element, so
    alert(this.value)
    //displays the value of whichever textbox was updated
    }
    then you could do something like

    Code:
    function getUpdate() {
    switch(this.name) {
    case "area1": //do something
    case "area2": //etc
    }
    }
    Last edited by gusblake; 12-07-2009 at 02:43 PM.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,733
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Why not simply:-

    Code:
    <input type = "text" name = "area1" onchange = "update(1)">
    <input type = "text" name = "area2" onchange = "update(2)">
    
    <script type = "text/javascript">
    function update(area) {
    if (area == 1) {
    // do this calculation
    }
    if (area == 2) {
    // do that calculation
    }
    }
    
    </script>
    Quizmaster: In mythology, what term for a potion that increases sexual desire is derived from the name of the Greek goddess of love?
    Contestant: Viagra

  • Users who have thanked Philip M for this post:

    nickyfraggle (12-08-2009)

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Posts
    200
    Thanks
    27
    Thanked 2 Times in 2 Posts
    Thanks! That works really nicely! Looks so simple when you know how hey?!

    Quick question - I got it working ok with a number to identify the field, but couldn't get it to respond to something like area1 - is there a way to do this (just out of curiosity)?

    I think I was treating it as a string, which didn't work but neither did using it without ""!

    Thanks again for your help.

    Nicky

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,733
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Is this what you mean?


    Code:
    <input type = "text" name = "area1" onchange = "update('area1')">
    <input type = "text" name = "area2" onchange = "update('area2')">
    
    <script type = "text/javascript">
    function update(areaName) {
    if (areaName == "area1") {
    // do this calculation
    }
    if (areaName == "area2") {
    // do that calculation
    }
    }
    
    </script>

  • Users who have thanked Philip M for this post:

    nickyfraggle (12-08-2009)

  • #6
    Regular Coder
    Join Date
    Mar 2006
    Posts
    200
    Thanks
    27
    Thanked 2 Times in 2 Posts
    Yes, but when I tried that it didn't work! Must have been doing something wrong!!

    Thanks again for your help!

    Nicola

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,733
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by nickyfraggle View Post
    Yes, but when I tried that it didn't work! Must have been doing something wrong!!

    Thanks again for your help!

    Nicola
    onchange = "update('area1')" - perhaps wrong quotes?


  •  

    Posting Permissions

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