Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie help.

Status
Not open for further replies.

skothk

Programmer
Nov 22, 2007
2
0
0
GB
Hi,

I'm looking to have a number of fields auto completed depending on which drop-down option id selected from a menu.

I've create a small funtion to do this for me, but the 'if' statement throughs up an error on submit.

Can anyone help?

getElementById('l') is the drop-down field

<script type="text/javascript">
<!--
function getValue(l) {

var ulocation = document.getElementById('l').options[l.selectedIndex].value;

If(ulocation == "Aberdeen");{

document.forms['newstarter'].elements['company'].value = "AddressLine1";
document.forms['newstarter'].elements['street'].value = 'AddressLine2';
document.forms['newstarter'].elements['city'].value = 'AddressLine3';
document.forms['newstarter'].elements['postcode'].value = 'AddressLine4';

}else if(ulocation == "London");{

document.forms['newstarter'].elements['company'].value = 'AddressLine1';
document.forms['newstarter'].elements['street'].value = 'AddressLine2';
document.forms['newstarter'].elements['city'].value = 'AddressLine3';
document.forms['newstarter'].elements['postcode'].value = 'AddressLine4';

}else if(ulocation == "Perth");{

document.forms['newstarter'].elements['company'].value = 'AddressLine1';
document.forms['newstarter'].elements['street'].value = 'AddressLine2';
document.forms['newstarter'].elements['city'].value = 'AddressLine3';
document.forms['newstarter'].elements['postcode'].value = 'AddressLine4';

}else{

return false;

}

}
// -->
</script>

Many thanks,

skothk
 
well first off, you don't need the ";" at the end of a if statement.
Code:
not good!
 if ();{

 }
Code:
good!
 if (){

 }

also you used If (), use lowercase if ()

Code:
not good!
 If (){

 }
Code:
good!
 if (){

 }

also you can't start an id name with a number...

this is the code in working order...
Code:
<script type="text/javascript"> 
<!--
    function getValue(I) {

    var ulocation = I.value;

    If(ulocation == "Aberdeen"){

        document.forms['newstarter'].elements['company'].value = "AddressLine1";
        document.forms['newstarter'].elements['street'].value = 'AddressLine2';
        document.forms['newstarter'].elements['city'].value = 'AddressLine3';
        document.forms['newstarter'].elements['postcode'].value = 'AddressLine4';

        } else if(ulocation == "London"){

        document.forms['newstarter'].elements['company'].value = 'AddressLine1';
        document.forms['newstarter'].elements['street'].value = 'AddressLine2';
        document.forms['newstarter'].elements['city'].value = 'AddressLine3';
        document.forms['newstarter'].elements['postcode'].value = 'AddressLine4';

        }else if(ulocation == "Perth"){

        document.forms['newstarter'].elements['company'].value = 'AddressLine1';
        document.forms['newstarter'].elements['street'].value = 'AddressLine2';
        document.forms['newstarter'].elements['city'].value = 'AddressLine3';
        document.forms['newstarter'].elements['postcode'].value = 'AddressLine4';

        } else{
        return false;

        }

    }
// -->
</script>
</head>

<body>

<form method="POST" name="newstarter">
  <p><select size="1" name="I" onchange="getValue(this);">
  <option value="Other">Other</option>
  <option value="Perth">Perth</option>
  <option value="London">London</option>
  <option value="Aberdeen">Aberdeen</option>
  </select></p>
  <p><input type="text" name="company" size="20"></p>
  <p><input type="text" name="street" size="20"></p>
  <p><input type="text" name="city" size="20"></p>
  <p><input type="text" name="postcode" size="20"></p>
  <p>&nbsp;</p>
  <p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>

</body>


~Nate_Bro


Code Snippets just because
 
Thank you Nate_Bro.

I did a straight copy/paste of your suggested code and I receive an error when the page loads:

############################

A Runtime Error has occurred.
Do you wish to Debug?

Line 11
Error: Expected ';'

############################

I'm using IE 6.0 as if you paste into a notepad or whatever, you'll see there is nothing on line 11!?

############################

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns="<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">
<!--
function getValue(I) {

var ulocation = I.value;

If(ulocation == "Aberdeen"){

document.forms['newstarter'].elements['company'].value = "AddressLine1";
document.forms['newstarter'].elements['street'].value = 'AddressLine2';
document.forms['newstarter'].elements['city'].value = 'AddressLine3';
document.forms['newstarter'].elements['postcode'].value = 'AddressLine4';

}else if(ulocation == "London"){

document.forms['newstarter'].elements['company'].value = 'AddressLine1';
document.forms['newstarter'].elements['street'].value = 'AddressLine2';
document.forms['newstarter'].elements['city'].value = 'AddressLine3';
document.forms['newstarter'].elements['postcode'].value = 'AddressLine4';

}else if(ulocation == "Perth"){

document.forms['newstarter'].elements['company'].value = 'AddressLine1';
document.forms['newstarter'].elements['street'].value = 'AddressLine2';
document.forms['newstarter'].elements['city'].value = 'AddressLine3';
document.forms['newstarter'].elements['postcode'].value = 'AddressLine4';

} else{
return false;

}

}
// -->
</script>
</head>

<body>

<form method="POST" name="newstarter">
<p><select size="1" name="I" onchange="getValue(this);">
<option value="Other">Other</option>
<option value="Perth">Perth</option>
<option value="London">London</option>
<option value="Aberdeen">Aberdeen</option>
</select></p>
<p><input type="text" name="company" size="20"></p>
<p><input type="text" name="street" size="20"></p>
<p><input type="text" name="city" size="20"></p>
<p><input type="text" name="postcode" size="20"></p>
<p>&nbsp;</p>
<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>

</body>

</html>

############################

Any further help would be much appreciated.

skothk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top