Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
help with a javascipt....heeeelppp
Old 01-10-2009, 04:46 PM help with a javascipt....heeeelppp
Junior Talker

Posts: 4
Trades: 0
Hi all,
This javascript show a drop down list with counties and after selecting a country show the state.
1.There is problem because I use it into a form and the second field (field4) submit a blank value.
2.In first drop down list is possible to have a default country, can make it to have the same thing for the second dropdown list?

Code:
<script type="text/javascript" language="javascript">
var postState = '';
var postCountry = '';

var state = '\
US:NY:New York|\
US:LA:Los Angelos|\
US:CO:Colorado|\
UK:LO:London|\
UK:LI:Liverpool|\
';
var country = '\
US:United State|\
UK:United Kingdom|\
';
function TrimString(sInString) {
  if ( sInString ) {
    sInString = sInString.replace( /^\s+/g, "" );// strip leading
    return sInString.replace( /\s+$/g, "" );// strip trailing
  }
}
// Populates the country selected with the counties from the country list
function populateCountry(defaultCountry) {
  if ( postCountry != '' ) {
    defaultCountry = postCountry;
  }
  var countryLineArray = country.split('|');  // Split into lines
  var selObj = document.getElementById('countrySelect');
  selObj.options[0] = new Option('Select Country','');
  selObj.selectedIndex = 0;
  for (var loop = 0; loop < countryLineArray.length; loop++) {
    lineArray = countryLineArray[loop].split(':');
    countryCode  = TrimString(lineArray[0]);
    countryName  = TrimString(lineArray[1]);
    if ( countryCode != '' ) {
      selObj.options[loop + 1] = new Option(countryName, countryCode);
    }
    if ( defaultCountry == countryCode ) {
      selObj.selectedIndex = loop + 1;
    }
  }
}
function populateState() {
  var selObj = document.getElementById('stateSelect');
  var foundState = false;
  // Empty options just in case new drop down is shorter
  if ( selObj.type == 'select-one' ) {
    for (var i = 0; i < selObj.options.length; i++) {
      selObj.options[i] = null;
    }
    selObj.options.length=null;
    selObj.options[0] = new Option('Select State','');
    selObj.selectedIndex = 0;
  }
  // Populate the drop down with states from the selected country
  var stateLineArray = state.split("|");  // Split into lines
  var optionCntr = 1;
  for (var loop = 0; loop < stateLineArray.length; loop++) {
    lineArray = stateLineArray[loop].split(":");
    countryCode  = TrimString(lineArray[0]);
    stateCode    = TrimString(lineArray[1]);
    stateName    = TrimString(lineArray[2]);
  if (document.getElementById('countrySelect').value == countryCode && countryCode != '' ) {
    // If it's a input element, change it to a select
      if ( selObj.type == 'text' ) {
        parentObj = document.getElementById('stateSelect').parentNode;
        parentObj.removeChild(selObj);
        var inputSel = document.createElement("SELECT");
        inputSel.setAttribute("name","state");
        inputSel.setAttribute("id","stateSelect");
        parentObj.appendChild(inputSel) ;
        selObj = document.getElementById('stateSelect');
        selObj.options[0] = new Option('Select State','');
        selObj.selectedIndex = 0;
      }
      if ( stateCode != '' ) {
        selObj.options[optionCntr] = new Option(stateName, stateCode);
      }
      // See if it's selected from a previous post
      if ( stateCode == postState && countryCode == postCountry ) {
        selObj.selectedIndex = optionCntr;
      }
      foundState = true;
      optionCntr++
    }
  }
  // If the country has no states, change the select to a text box
  if ( ! foundState ) {
    parentObj = document.getElementById('stateSelect').parentNode;
    parentObj.removeChild(selObj);
  // Create the Input Field
    var inputEl = document.createElement("INPUT");
    inputEl.setAttribute("id", "stateSelect");
    inputEl.setAttribute("type", "text");
    inputEl.setAttribute("name", "state");
    inputEl.setAttribute("size", 20);
    inputEl.setAttribute("value", postState);
    parentObj.appendChild(inputEl) ;
  }
}
function initCountry(country) {
  populateCountry(country);
  populateState();
}
</script>
HTML

<select id='countrySelect' name='field3' onchange='populateState()'>
        </select>
           <select id='stateSelect' name='field4'>
        </select>
        <script type="text/javascript">initCountry('US'); </script>
gonny is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-10-2009, 04:59 PM Re: help with a javascipt....heeeelppp
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
set selectedIndex to the default selection
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-10-2009, 05:36 PM Re: help with a javascipt....heeeelppp
Junior Talker

Posts: 4
Trades: 0
what do you mean?
Sorry, but I'm not so familiar with javascript...
gonny is offline
Reply With Quote
View Public Profile
 
Old 01-10-2009, 06:50 PM Re: help with a javascipt....heeeelppp
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
you already do exactly what you need to do in the first one.

Code:
    if ( defaultCountry == countryCode ) {
      selObj.selectedIndex = loop + 1;
    }
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-11-2009, 07:38 AM Re: help with a javascipt....heeeelppp
Junior Talker

Posts: 4
Trades: 0
Ok thanx chrishirst.
Now I solve to define default state but can't submit the values .
How I can define value='' for each option??
gonny is offline
Reply With Quote
View Public Profile
 
Old 01-11-2009, 09:17 AM Re: help with a javascipt....heeeelppp
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
If you have no idea how this works, who wrote the original code? and do we get a link to the page that is [not] working?
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-13-2009, 12:56 PM Re: help with a javascipt....heeeelppp
Junior Talker

Posts: 4
Trades: 0
I got it here but now I solve it.
Thnx for your attention.
gonny is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to help with a javascipt....heeeelppp
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.26514 seconds with 12 queries