SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: javascript n00b

  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    US
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    javascript n00b

    i'm just getting familiar with javascript. i have a problem, i'm designing a script that takes a number from a disabled text box in a form (which has an initial value of 1) and on pressing a button (on the same form) increments the value in the disabled text box. this is the code i have, but everytime i click on the button, it just address to the #, e.g. it'll be 1111 instead of 4.... i googled this thing but to no avail. here's my script:

    <script language="JavaScript">
    function testresults(form){

    var initvalue = form.calls.value; //get value from textbox calls

    if(initvalue==0){
    form.calls.value = 0;
    } else {
    form.calls.value = initvalue + 1;
    }
    }
    </script>
    <form name="form" method="post" action="">
    Calls
    <input name="calls" type="text" disabled value="1" >
    <input type="button" name="update" value="Submit" onClick="testresults(this.form)">
    </form>

    please nudge me the right direction. TIA

  2. #2
    SitePoint Evangelist luxinterior's Avatar
    Join Date
    Aug 2004
    Location
    Here, there and everywhere!
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to make it appear as an integer and not a string.

    <script language="JavaScript">
    function testresults(form){

    var initvalue = parseInt(form.calls.value); //get value from textbox calls

    if(initvalue==0){
    form.calls.value = 0;
    } else {
    initvalue+=1;
    form.calls.value=initvalue;
    }
    }
    </script>

    Lux

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    NJ
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the simplest thing that you could do, I had the same kinda problem, is multiple it by 1 to 'force' it to integer:

    form.calls.value = (initvalue*1) + (1*1);

    I'm not sure whether or not the one also needs to multiplied by one...but just to be on the safe side...Anyway I have to be honest, I'm kinda replying to also promote the answering of my own apparently complex question. So if anyone who is reading this wants a challenge, I would gladly welcome you to try to figure out my problem, here the link:
    http://www.sitepoint.com/forums/show...47#post1495347
    thx

  4. #4
    SitePoint Addict markchivs's Avatar
    Join Date
    Oct 2004
    Location
    Malvern Hills, UK
    Posts
    233
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hddd12345678910
    the simplest thing that you could do, I had the same kinda problem, is multiple it by 1 to 'force' it to integer:

    form.calls.value = (initvalue*1) + (1*1);

    I'm not sure whether or not the one also needs to multiplied by one...but just to be on the safe side...Anyway I have to be honest, I'm kinda replying to also promote the answering of my own apparently complex question. So if anyone who is reading this wants a challenge, I would gladly welcome you to try to figure out my problem, here the link:
    http://www.sitepoint.com/forums/show...47#post1495347
    thx
    So why not just use parseInt ?

    Seeing that it's a in built javaScript function that will also validate wether or not the string is NAN or whether it is. I would come to your code example and think "Why on earth is this developer multiplying by 1?"

    Do The Simplest Thing That Can Possibly Work!!!!




Bookmarks

Posting Permissions

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