Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2006
    Posts
    45
    Thanks
    1
    Thanked 0 Times in 0 Posts

    What's wrong with this?

    Hi,

    I have the following code:

    Code:
    var makelist=document.myForm.make;
    var modellist=document.myForm.model;
    
    var newModel = getModelsByMake("Ford");
    document.write("newModel :" +newModel + "<br /><br />");
    var newModel2 = getModelsByMake("Nissan");
    document.write("newModel2 :" +newModel2 + "<br /><br />");
    var newModel3 = getModelsByMake("Toyota");
    document.write("newModel3 :" +newModel3 + "<br /><br />");
    
    
    var model=new Array();
    model[0]="";	
    model[1]=getModelsByMake("Ford");
    model[2]=getModelsByMake("Nissan");
    model[3]=getModelsByMake("Toyota");
    
    
    function updatemodel(selectedMake){
    	modellist.options.length=0;
    	if (selectedMake>0){
    		for (i=0; i<model[selectedMake].length; i++){
    		modellist.options[modellist.options.length]=new Option(model[selectedMake][i].split("|")[0], model[selectedMake][i].split("|")[1]);
    		}
    	}
    }
    When I write to the screen the getModelsByMake(mType) with the parameters it outputs the correct results. On the other hand, when I place the getModelsByMake(mType) in the model[1],model[2],etc... arrays, it doesn't contain the same data as the printed screen version of the same function and parameter. Is the problem with the updatemodel(selectedmake) function? Is this the reason the drop down menu contains the same 2 vehicles regardless of what you pick in the first drop down?

    Here is the code for the getModelsByMake(mType) Function:

    Code:
    var myAutos = new Array();
    
    myAutos[0] = new automobile("Ford","Crown Victoria",1999,156789);
    myAutos[1] = new automobile("Ford","Crown Victoria",2000,89652);
    myAutos[2] = new automobile("Ford","Crown Victoria",2001,22354);
    myAutos[3] = new automobile("Ford","Crown Victoria",2002,105236);
    myAutos[4] = new automobile("Ford","Crown Victoria",2003,12457);
    myAutos[5] = new automobile("Ford","Crown Victoria",1999,36201);
    myAutos[6] = new automobile("Ford","Taurus",2002,64512);	
    myAutos[7] = new automobile("Ford","Taurus",1999,126789);
    myAutos[8] = new automobile("Ford","Taurus",2000,45652);
    myAutos[9] = new automobile("Ford","Taurus",2001,37354);
    myAutos[10] = new automobile("Ford","Taurus",2002,125236);
    myAutos[11] = new automobile("Ford","Taurus",2003,42457);
    myAutos[12] = new automobile("Ford","Taurus",1999,28201);
    myAutos[13] = new automobile("Ford","Taurus",2002,54512);
    myAutos[14] = new automobile("Nissan","Pathfinder",1999,156789);
    myAutos[15] = new automobile("Nissan","Pathfinder",2000,89652);
    myAutos[16] = new automobile("Nissan","Pathfinder",2001,22354);
    myAutos[17] = new automobile("Nissan","Pathfinder",2002,105236);
    myAutos[18] = new automobile("Nissan","Pathfinder",2003,12457);
    myAutos[19] = new automobile("Nissan","Pathfinder",1999,36201);
    myAutos[20] = new automobile("Nissan","Frontier",2002,64512);	
    myAutos[21] = new automobile("Nissan","Frontier",1999,126789);
    myAutos[22] = new automobile("Nissan","Frontier",2000,45652);
    myAutos[23] = new automobile("Nissan","Frontier",2001,37354);
    myAutos[24] = new automobile("Nissan","Frontier",2002,125236);
    myAutos[25] = new automobile("Nissan","Frontier",2003,42457);
    myAutos[26] = new automobile("Nissan","Frontier",1999,28201);
    myAutos[27] = new automobile("Nissan","Frontier",2002,54512);	
    myAutos[28] = new automobile("Toyota","Tacoma",2002,37912);	
    
    function getModelsByMake(mType) {
    	this.mType = mType;
      var ta = [];
      for(var i = 0; i<myAutos.length; ++i){
        if(myAutos[i].make === mType){
          ta.push(myAutos[i].model+"|"+myAutos[i].model); 
    		}
    		
    	}
    		var md = ta[0];
    		models[0] = md;
    		mdIndex = 0;
    		for(var i = 0; i < ta.length; i++) {
    				if(ta[i] == md){
    					continue;
    				}
    				else{
    					md = ta[i];
    					models[++mdIndex] = md;
    				}
    		}
    		return models;
    }

  • #2
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    THe first thing I notice is that you haven't declared your models array, you just start using it.

    Also you have a model array and a models array. Perhaps that's your problem, you're thinking they are the same but they are not.

  • #3
    New Coder
    Join Date
    Jul 2006
    Posts
    45
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Got it! It seems to be working fine now.

    Thank you so much for all your help! I couldn't have done it without your help and Twey's help from the dynamicDrive forum. You guys are the best and I really appreciate everything.

    I may be coming back to this forum for more help, I hope not to be aggrevating you or anyone else who may want to help. I am new to JS and getting the syntax in my head has been difficult. I am used to ColdFusion which can generate JS for forms etc... but I wanted to learn how to write my own scripts or modify scripts off the net which is why I took a class. These forums will be a great help in my journey to learn JS escpesially with people willing to help like yourself.

    Thanks Again,

    7079

  • #4
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    Not a bother at all. Intelligent questions and and inquisitive minds are always welcome here. Just make sure to do the legwork yourself and put as much effort into asking your question as you expect people to put in answering it.

    Hopefully some day you can pass on the favor and deliver good advice to the new wave of newbies. It's how we all got started.


  •  

    Posting Permissions

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