Results 1 to 4 of 4

Thread: [RESOLVED] javascript listbox not working

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    Resolved [RESOLVED] javascript listbox not working

    I've made a taxi fare calculator using google maps, and now im trying to add extra functionality, but i cant get the following code to work.

    The idea is that once its calculated and displayed the fare in "txtFare", then to find the fare for a "bus", its selected from the list box and multiply's "txtFare" by '1.50' and displays it in "txtTypeFare".

    This function doesn't work, and i can't see why not.
    Code:
    	function calcType() {
       	var VehType = document.getElementById("sVehicle").innerHTML;
    	var EstFare = document.getElementById("txtFare").value;
    	
        	if (VehType = "car")
        		{document.getElementById("txtTypeFare").value = EstFare};
        	if (VehType = "estate")
        	   	{document.getElementById("txtTypeFare").value = EstFare * 1.25 };
        	if (VehType = "bus")
           		{document.getElementById("txtTypeFare").value = EstFare * 1.50};
        	if (VehType = "exec")
    	       	{document.getElementById("txtTypeFare").value = EstFare * 1.75};
    	}
    HTML Code:
        <b>Fare: </b>
    		<input id="txtFare" type="textbox" value="">        
            <input type="button" value="fare" onClick="calcFare();">
        <b>Vehicle: </b>
    		<select id="sVehicle" onChange="calcType()">
    		  <option value="car">Car</option>
    		  <option value="estate">Estate</option>
    		  <option value="bus">Mini-Bus</option>
    		  <option value="exec">Executive</option>		  
    		</select>
    		<input id="txtTypeFare" type="textbox" value=""> 
    Thanks in advance
    If your question is answered then mark your thread RESOLVED and give credit to whoever answered it.

    If you fail, try and try again, its the only way to success.

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: javascript listbox not working

    This line is a problem:
    Code:
    var VehType = document.getElementById("sVehicle").innerHTML;
    If you want to see what the VehType variable is being populated with, add alert(VehType); on the line after that; it's not what you want.

    Switch to:
    Code:
    var VehType = document.getElementById("sVehicle").value;
    Also, on your if statements, you're using = instead of ==. That means, in all of your if statements, VehType is being assigned a value, not evaluated for a value, and all of your if statements are evaluating to true (because variable assignment evaluates to true). So the only if statement that you'll see the effect of is the last one.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    Greater Manchester, UK
    Posts
    476

    Re: javascript listbox not working

    thanks for the advice on ==, its working correctly now.

    chris1990
    If your question is answered then mark your thread RESOLVED and give credit to whoever answered it.

    If you fail, try and try again, its the only way to success.

  4. #4
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: javascript listbox not working

    If your question is answered then mark your thread RESOLVED and give credit to whoever answered it.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

Posting Permissions

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



Click Here to Expand Forum to Full Width