Javascript - based on selection

Discussion in 'HTML & Website Design' started by AstarothSolutions, Jul 9, 2007.

  1. #1
    I have a form element (country)

    I need to create an onchange javascript script such that if country = "USA" then the form element "state" is visible, if country = anything else then state is to be invisible and "county" is to be visible

    This should be very easy but I am pants at javascript
     
    AstarothSolutions, Jul 9, 2007 IP
  2. JOGS_DEV

    JOGS_DEV Peon

    Messages:
    136
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Well, if you are displaying countries as a drop-down list, then you can do following:
    1. include onchange event in the <select> tag, as in here:

    ...<select name="country" onchange="check(this.value)">...
    Code (markup):
    3. where you want your state/county elements, create empty div with some id, as in here:

     <div id="state_county"></div>
    Code (markup):
    2. include following function in the head tag,
      function check(x)
     {
       if (x=="USA") {
    	document.getElementById("state_county").innerHTML='...your code to display state field...'
       } else {
    	document.getElementById("state_county").innerHTML='...your code to display county filed...'
       }
     }
    
    Code (markup):
     
    JOGS_DEV, Jul 9, 2007 IP
  3. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Many thanks

    My error was using "name" instead of "id" attributes in the form elements
     
    AstarothSolutions, Jul 10, 2007 IP