/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] javascript not working for me

so heres my page:

[code=html]<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”content-type” content=”text/html; charset=utf-8″ />
<title>LoraxAlliance</title>
<link href=”includes/style.css” rel=”stylesheet” type=”text/css” />
<script type=”text/javascript” src=”http://www.loraxalliance.org/includes/coutry.js”></script></head>
<body>
<table id=”page”>
<tr>
<td id=”header” colspan=”5″></td>
</tr>
<tr>
<td id=”button”>
<a href=”http://www.loraxalliance.org/”><img src=”http://www.loraxalliance.org/images/Home.gif”></a>
</td>
<td id=”button”>
<a href=”http://www.loraxalliance.org/clubs.php”><img src=”http://www.loraxalliance.org/images/Clubs.gif”></a>
</td>
<td id=”button”>
<a href=”http://www.loraxalliance.org/projects.php”><img src=”http://www.loraxalliance.org/images/Projects.gif”></a>
</td>
<td id=”button”>
<a href=”http://www.loraxalliance.org/forums.php”><img src=”http://www.loraxalliance.org/images/Forums.gif”></a>
</td>
<td id=”button”>
<a href=”http://www.loraxalliance.org/login.php”><img src=”http://www.loraxalliance.org/images/Login.gif”></a>
</td>
</tr>
<tr id=”main”>
<td id=”menu”></td>
<td id=”content” colspan=”3″>
<form method=”post” id=”form”><br />
<table align=”center” id=”ftable”>
<tr>
<td>First Name*:</td>
<td><input type=”text” name=”first” /></td>
</tr>
<tr>
<td>Last Name*:</td>
<td><input type=”text” name=”last” /></td>
</tr>
<tr>
<td>Make name public:</td>
<td><input type=”checkbox” name=”pname” checked></td>
</tr>
<tr>
<td>Username*:</td>
<td><input type=”text” name=”user” /></td>
</tr>
<tr>
<td>Password*:</td>
<td><input type=”password” name=”password” /></td>
</tr>
<tr>
<td>Confirm Password*:</td>
<td><input type=”password” name=”password2″ ></td>
</tr>
<tr>
<td>Email*:</td>
<td><input type=”text” name=”email” ></td>
</tr>
<tr>
<td>Confirm email*:</td>
<td><input type=”text” name=”email2″ ></td>
</tr>
<tr>
<td>Make email public:</td>
<td><input type=”checkbox” name=”pemail” checked></td>
</tr>
<tr>
<td>School*:</td>
<td><input type=”text” name=”school” ></td>
</tr>
<tr>
<td>Make school public:</td>
<td><input type=”checkbox” name=”pschool” checked</td>
</tr>
<tr id=”loc”>
<td id=”coun”>
<select id=”countrySelect” name=”country” onchange=”populateState()”></select>
</td>
<td id=”sta”>
<select id=”stateSelect” name=”state”></select>
<script type=”text/javascript”>initCountry(‘US’); </script>
</td>
</tr>
<tr>
<td>Please write a little about yourself.</td>
<td><textarea cols=”30″ rows=”5″ name=”about”></textarea></td>
</tr>
<tr>
<td>Enter the string below exactly as you see it.<br>(there are no ones or zeros.)</td>
</tr>
<tr>
<td><img src=”http://www.loraxalliance.org/includes/val.php” border=0></td>
<td><input type=”text” name=”conf”></td>
</tr>
<tr>
<td colspan=”2″><center><input type=”submit” value=”Register” name=”reg”/></center></td>
</tr>
</table>
</form>
</td>
<td id=”posts”></td>
</tr>
<tr>
<td id=”footer” colspan=”5″>Copyright 2008</td>
</tr>
</table>
</body>
</html>[/code]

and here is the javascript at “http://www.loraxalliance.org/includes/coutry.js” (the strings with the states are out to shorten it):

[CODE]
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();
}[/CODE]

but the selects are blank.

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@Angry_Black_ManAug 09.2008 — what errors are you getting?
Copy linkTweet thisAlerts:
@nickmagusauthorAug 10.2008 — the fields that are supposed to change don't change. the javascript works on other pages but not on mine for some reason. the select menus just stay empty.
Copy linkTweet thisAlerts:
@Angry_Black_ManAug 10.2008 — okay, but what errors are you getting?
Copy linkTweet thisAlerts:
@nickmagusauthorAug 10.2008 — none that i see how would i see errors?
Copy linkTweet thisAlerts:
@Declan1991Aug 10.2008 — Firefox, Opera and Safari all have error consoles (In Firefox, Tools->Error Console), IE is the only browser that doesn't.

I only gave it a quick glance, but I think you have a referencing problem, but I'm not sure. I don't see where most of the variables are defined.
function populateCountry(defaultCountry) {
if ( [B]postCountry[/B] != '' ) {
defaultCountry = postCountry;
}
var countryLineArray = [B]country[/B].split('|'); // Split into lines
}
Copy linkTweet thisAlerts:
@nickmagusauthorAug 11.2008 — those variables all exist they just made my post too long to post so i left them out. country is the list of all countries and likewise for state. and post country and state are the post values of them.
Copy linkTweet thisAlerts:
@nickmagusauthorAug 11.2008 — figured it out i had it importing coutry.js which didn't exist instead of country.js thanks. i just couldn't figure that out cause i didn't know about the error consol.
×

Success!

Help @nickmagus spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...