SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member kantutan's Avatar
    Join Date
    Apr 2008
    Location
    Here
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help: Modifying arrays

    On my webpage I have 3 text boxes (txt1, txt2, txt3) & a button. My Javascript is such that if I enter 's' into txt1 & click the button the values '1' & '2' in the array shows in txt2 & txt3; same with 'm' array

    Code:
    <HTML>
    <HEAD>
    <TITLE>JavaScript</TITLE>
    <SCRIPT LANGUAGE="JAVASCRIPT">
    var data = {
      s : [1, 2],
      m : [3, 4]
    }
    
    function foo(){
    
      var userInput = document.forms[0].txt1.value;
      var vals = data[userInput];
      var val1="";
      var val2="";
    
      if(vals!=null){
        val1 = vals[0];
        val2 = vals[1];
      }
    
      document.forms[0].txt2.value = val1;
      document.forms[0].txt3.value = val2;
    
    }
    </script>
    </HEAD>
    <BODY bgcolor="blue">
    <form>
    <input type = text name=txt1 id="txt1" value="" /><br>
    <input type="button" value="button" name="button" onclick="return foo()"/><br>
    <input type = text name=txt2 id="txt2" value="" /><br>
    <input type = text name=txt3 id="txt3" value="" />
    </form>
    </BODY>
    </HTML>
    My problem is: How do I modify this Javascript such that instead of 's' & 'm', I use a range of numbers e.g. 10.5-12.5 for [1, 2], 12.6-14.5 for [3, 4]?

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,687
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    Code javascript:
    userInput = Number(userInput);
    if (userInput >= 10.5 && userInput <= 12.5) {
        // use the first range
    } else if (userInput >= 12.6 && userInput <= 14.5) {
        // use the second range
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •