salman233 Posted April 23, 2012 Share Posted April 23, 2012 hello guys i want a little help with multidimensional arrays actually em making a currency convert with java script em stuck at point where i want to print a multidimensional array my code actually em a newbie <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script type="text/javascript"> var Country = new Array('Pakistan','USA'); // pakistan Country['Pakistan'] = new Array('Punjab','Sindh'); Country['Pakistan']['Punjab'] = new Array('Faisalabad','Lahore','Multan'); Country['Pakistan']['Singh'] = new Array('Karachi'); // USA Country['USA'] = new Array('New York'); Country['USA']['New York'] = new Array('New York','New York2'); ///////////////////////// function changecountry(value){ try{ var state_area = document.getElementById('state_area'); var state_drop = document.getElementById('state_drop'); if(value==''){ state_drop.innerHTML = ''; state_area.style.display = 'none'; //city_drop.innerHTML = ''; //city_area.style.display = 'none'; //empty check return false; } if(typeof Country[value] == 'undefined') return false; var newState = document.createElement('select'); newState.name = 'state'; newState.onChange = function (){ alert("hello") } var State = Country[value]+[value]; here is where i want to call country[state][city] from multi dimensional array and print throught for loop //alert(typeof nsewState.appendChild) for(var i=0,j=0; i<State.length,j<State[j].length; i++,j++){ var newoptState = document.createElement('option'); newoptState.value = State[i]; newoptState.innerHTML = State[i]; newState.appendChild(newoptState); } state_drop.innerHTML = ''; state_drop.appendChild(newState); state_area.style.display = 'block'; }catch(e){ alert(e.message) } } function changecity(value){ try{ var city_area = document.getElementById('city_area'); var city_drop = document.getElementById('city_drop'); if(value==''){ city_drop.innerHTML = ''; city_area.style.display = 'none'; //empty check return false; } if(typeof Country[value] == 'undefined') return false; var newCity = document.createElement('select'); newCity.name = 'city'; newCity.onChange = function (){ alert(23) } var City = Country[value]; //alert(typeof newState.appendChild) for(var i=0; i<City.length; i++){ var newoptCity = document.createElement('option'); newoptCity.value = City[i]; newoptCity.innerHTML = City[i]; newCity.appendChild(newoptCity); } city_drop.innerHTML = ''; city_drop.appendChild(newCity); city_area.style.display = 'block'; }catch(e){ alert(e.message) } } </script> </head> <body> <form> Country: <select name="country" onChange="changecountry(this.value)"> <option value="">Select</option> <option value="Pakistan">Pakistan</option> <option value="USA">USA</option> </select> <br> <div id="state_area" style="display:none;" onChange="changecity(this.value)">State: <span id="state_drop" onChange="changecity(this.value)"></span></div> <div id="city_area" style="display:none;">City: <span id="city_drop"></span></div> </form> </body> </html> i want to call country[state][city] through for loop but gives a error but when i call country[state] only it,s displays country and onclick displays states please help me out where em wrong Quote Link to comment Share on other sites More sharing options...
salman233 Posted April 24, 2012 Author Share Posted April 24, 2012 anybody please help :'( Quote Link to comment Share on other sites More sharing options...
haku Posted April 24, 2012 Share Posted April 24, 2012 You didn't ask a question, and you posted a whole bunch of code. Ask a specific question, and narrow your code down to the relevant parts, and someone may help. Quote Link to comment Share on other sites More sharing options...
Maq Posted April 24, 2012 Share Posted April 24, 2012 salman, in the future, place OR tags around your code. Quote Link to comment Share on other sites More sharing options...
salman233 Posted April 28, 2012 Author Share Posted April 28, 2012 ok thanks guys question in short is that this is a var State= country['value'] if i print it with for loop it displays result now further printing it like this var State2= country['value1']['value2'] with for loop it don,t shows any result please tell me where i am wrong ? Quote Link to comment Share on other sites More sharing options...
salman233 Posted April 29, 2012 Author Share Posted April 29, 2012 hello Quote Link to comment Share on other sites More sharing options...
haku Posted April 29, 2012 Share Posted April 29, 2012 1) Tell us what you have, what your current code is doing 2) Tell us what you want your code to do 3) Show us the code that you currently have that is causing you the problem 4) Give an example of what the desired output is If you do these four things, it will be much easier to help you. Quote Link to comment Share on other sites More sharing options...
salman233 Posted April 30, 2012 Author Share Posted April 30, 2012 var Country = new Array('Pakistan','USA'); // pakistan Country['Pakistan'] = new Array('Punjab','Sindh'); Country['Pakistan']['Punjab'] = new Array('Faisalabad','Lahore','Multan'); Country['Pakistan']['Singh'] = new Array('Karachi'); // USA Country['USA'] = new Array('New York'); Country['USA']['New York'] = new Array('New York','New York2'); var State = Country[value][value]; for(i=0; i<State.length; i++) { document.write(State); } this code displays nothing i want to display multidimensional array using for loop but it displays nothing Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 30, 2012 Share Posted April 30, 2012 var State = Country[value][value]; What the heck is that line doing? Quote Link to comment Share on other sites More sharing options...
salman233 Posted April 30, 2012 Author Share Posted April 30, 2012 i stated above em a newbie correct me where em wrong Quote Link to comment Share on other sites More sharing options...
haku Posted May 1, 2012 Share Posted May 1, 2012 How? You never answered any of my questions, and it's impossible to know what you are trying to do. Quote Link to comment Share on other sites More sharing options...
salman233 Posted May 2, 2012 Author Share Posted May 2, 2012 ok haku bro it,s like this i want to print countries using for loop i have new array country= new array('pakistan','usa'); ok now i want to print states of both countries['pakistan'] = new array ('punjab','sindh); country['usa']= new array ('bla','bla'); i want to do this using for loop my current code is shown above it displays nothing i want to display stated and countries meaning i want to print whole array Quote Link to comment Share on other sites More sharing options...
salman233 Posted May 5, 2012 Author Share Posted May 5, 2012 hello i am waiting Quote Link to comment Share on other sites More sharing options...
haku Posted May 6, 2012 Share Posted May 6, 2012 So am I: 1) Tell us what you have, what your current code is doing 2) Tell us what you want your code to do 3) Show us the code that you currently have that is causing you the problem 4) Give an example of what the desired output is If you do these four things, it will be much easier to help you. Quote Link to comment Share on other sites More sharing options...
goodacre.liam Posted May 19, 2012 Share Posted May 19, 2012 This thread is brilliant. Also, I'm sorry mate, I've tried going through your code but it is pretty unfathomable. Although I think you may be wanting to use an object instead of an array: var country = { 'Pakistan' : { 'Punjab' : ['Faisalabad','Lahore','Multan'], 'Sindh' : ['Karachi'] }, 'USA' : { 'New York': ['New York','New York2'] } }; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.