Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 4 of 4
  1. #1
    Senior Member
    Join Date
    Sep 2011
    Location
    South Africa
    Posts
    142
    Member #
    29386

    Some help with functions and forms

    The code below I am trying to test for user to enter salary but when I enter a value less than 5000 or more than 4999 it still says You do not qualify for a credit card. Why? I tried various combinations of the comparison operators >, >=, <, <= and it still just fronted out "You do not qualify for a credit card"


    Please help me with this. Thanks in advance.


    Code:
    <script type="text/javascript">        
                    /* <=!=[=C=D=A=T=A=[ */
            /* ]=]=> */
                var salary = function(earnings) {
                    
                    if (document.getElementById(earnings) > 4999) {
                        alert("You qualify for a credit card! Please come in.");
                    } else if (document.getElementById(earnings) < 5000 ) {    
                        alert("You do not qualify for a credit card");
                    } else {
                        alert("You did not enter anything");
                    }
                    
                };
                
            </script>

    HTML Code:
    <form action="">            
    <p><input type="text" placeholder="Enter salary" maxlength="30" id="earnings" /></p>            
    <p><input type="submit" value="Enter" onclick="salary(earnings)" /></p>                    
    </form>

  2.  

  3. #2
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,485
    Member #
    425
    Liked
    2783 times
    You're entering earnings as a variable when you don't have to.

    Change your onclick to this:
    Code:
    salary(document.getElementById('earnings'))
    That will pass the field itself to the salary function, rather than a variable with nothing in it.
    Then change your function to this (notice how "salary" isn't a variable):
    Code:
    function salary(earningsField) {
          var earnings = earningsField.value; / * this will get the value from the field.  I'll leave the rest up to you. */
    }
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  4. #3
    Senior Member
    Join Date
    Sep 2011
    Location
    South Africa
    Posts
    142
    Member #
    29386
    Great. Thanks much

    Here is what I did.

    Code:
    var salary = function() { // I removed the earnings parameter and changed it to a variable inside the function                       var earnings = document.getElementById('earnings').value; // earnings variable to retrieve values entered 
                    
                    if (earnings > 4999) { //and just used the variable earnings here instead of the doc.ElementById();
                        alert("You qualify for a credit card! Please come in.");
                    } else if (earnings < 5000) {    
                        alert("You do not qualify for a credit card");
                    } else {
                        alert("You did not enter anything");
                    }
                };

    HTML Code:
    <form action="">
    <p><input type="text" placeholder="Enter salary" maxlength="30" id="earnings"/></p>       
    <p><input type="submit" value="Enter" onclick="salary(document.getElementById('earnings'))" /></p>           
    </form>

  5. #4
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,485
    Member #
    425
    Liked
    2783 times
    I'm not sure why you're declaring salary as a variable (or even if it works...never tried it before), but if it works for you, then go for it I guess. Personally, I prefer function salary () so that I know it's a function when I read my code from left to right. But again, if that worked, cool.
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)


Remove Ads

Posting Permissions

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

Tags for this Thread

All times are GMT -6. The time now is 05:13 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com