Results 1 to 3 of 3

Thread: Else If statement problem

  1. #1
    Join Date
    Jun 2012
    Posts
    33
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Lightbulb Else If statement problem

    I have a bit of code where in the html there is a length and width text box and a submit button.

    The Java Script part adds up the length and width when the button is pressed and with the total it works out a price range depending on the value. it then displays the price in the html. It is not displaying the correct price and im wondering what has gone wrong. The code is below.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script type="text/javascript">
    function range()
    		
    		{
    		var dimension = (document.getElementById("length").value + document.getElementById("width").value);	
    		var from = 0;
    		var to = 0;
    		
    		
    			if (dimension < 3.5)
    			{
    			from = 900
    			to = 1125
    			}
    			
    			else if (dimension >=3.5 && dimension < 4)
    			{
    			from = 1200;
    			to = 1500;
    			}
    			else if (dimension >=4 && dimension < 5)
    			{
    			from = 1600;
    			to = 1900;
    			}
    			else if (dimension >=5)
    			{
    			from = 1950;
    			to = 2200;
    			}
    			else
    			{
    			alert("invalid entry");
    			}
    			document.getElementById("price").innerHTML= from + " - " + to;
    		}
    </script>
    
    </head>
    
    <body>
    
    <form name="bathroom" >
                  <table width="300" border="0" cellspacing="0" cellpadding="2">
                    <tr>
        <td width="164" height="26" class="textbold">Length</td>
        <td width="236"><label>
          <input name="length" type="text" class="text" id="length"/>
        </label></td>
      </tr>
      <tr>
        <td class="textbold">Width</td>
        <td><label>
          <input name="width" type="text" class="text" id="width"/>
        </label></td>
      </tr>
      <tr>
        <td class="textbold">&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td colspan="2" align="center"><div id="price"></div>
          </td>
      </tr>
      <tr>
        <td colspan="2" align="center"></td>
        </tr>
    </table>
                  <label>
                    <input type="button" name="ok" id="ok" value="Submit" onclick="range()"/>
      </label>
    </form>
    </body>
    </html>

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>Untitled Document</title>
            <script type="text/javascript">
            function range(){
            		var l= document.getElementById("length").value,w=document.getElementById("width").value;
                    if (l&&isFinite(l*1)&&w&&isFinite(w*1)){
    		         var dimension = 1*1 + w*1;  // original calculation
                     var dimension = l*w;        // I think this is what you want
                     var from = 0,to = 0;
    
    
            			if (dimension < 3.5){
            			 from = 900
            			 to = 1125
            			}
    
            			else if (dimension < 4){
            			 from = 1200;
            			 to = 1500;
            			}
            			else if (dimension < 5){
            			 from = 1600;
            			 to = 1900;
            			}
            			else {
            		  	 from = 1950;
            			 to = 2200;
            			}
            			document.getElementById("price").innerHTML= from + " - " + to;
                      }
            		  else{
            		   alert("invalid entry");
            		  }
            		}
            </script>
    
            </head>
    
            <body>
    
            <form name="bathroom" >
                          <table width="300" border="0" cellspacing="0" cellpadding="2">
                            <tr>
                <td width="164" height="26" class="textbold">Length</td>
                <td width="236"><label>
                  <input name="length" type="text" class="text" id="length"/>
                </label></td>
              </tr>
              <tr>
                <td class="textbold">Width</td>
                <td><label>
                  <input name="width" type="text" class="text" id="width"/>
                </label></td>
              </tr>
              <tr>
                <td class="textbold">&nbsp;</td>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td colspan="2" align="center"><div id="price"></div>
                  </td>
              </tr>
              <tr>
                <td colspan="2" align="center"></td>
                </tr>
            </table>
                          <label>
                            <input type="button" name="ok" id="ok" value="Submit" onclick="range()"/>
              </label>
            </form>
            </body>
            </html>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    Jun 2012
    Posts
    33
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default

    Well that works. Im not sure what you bit of code did to make it work, could you explain it to me? Don't worry this isnt any form of homework Im just new to java script.

Similar Threads

  1. Change syntax from Update Case Statement to Update Union Statement
    By newphpcoder in forum MySQL and other databases
    Replies: 0
    Last Post: 11-11-2011, 01:23 AM
  2. Replies: 3
    Last Post: 12-20-2010, 02:42 AM
  3. Resolved problem with friend system 'if' statement
    By liamallan in forum PHP
    Replies: 6
    Last Post: 04-10-2010, 04:58 PM
  4. Resolved WHERE Statement
    By X96 Web Design in forum MySQL and other databases
    Replies: 20
    Last Post: 05-09-2009, 11:50 PM
  5. Using an If Statement here...
    By tomyknoker in forum Flash
    Replies: 1
    Last Post: 03-27-2007, 01:16 PM

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
  •