Link to home
Start Free TrialLog in
Avatar of Richard Korts
Richard KortsFlag for United States of America

asked on

Why doesn't <select> fill onLoad?

I have this situation.

HTML:
<body onLoad="chk_models();">

Open in new window

<div class="row">
			<div class="col-sm-3 col-xs-3"><div style="padding-left:5px;"><u>Select Brand/Model</u></div></div>
			<div class="col-sm-3 col-xs-3">				<select name="ctbrand" onChange="get_mods();">
					<option value="">-- Select Brand --</option>
					<option value="BAC" >BAC</option>
					<option value="EVAPCO" selected>EVAPCO</option>
					<option value="SPX/MARLEY" >SPX/MARLEY</option>
					<option value="Other" >Other</option>
				</select>
				
			</div>
			<div class="col-sm-1 col-xs-1">&nbsp;</div>

			<div class="col-sm-3 col-xs-3">				<select name="ctmodel" id="ctmodel" onChange="get_dims();">
		<option value="">-- Select Model --</option>	
		</select><br><span style="visibility:hidden;"><input type="text" name="bother" size="14" id="bother"></span>
				
			</div>
		</div>

Open in new window

Javascript:
function chk_models() {
	if (document.st.ctbrand.value != "") {
		get_mods();
	}
}

Open in new window

function get_mods() {
	if (document.st.ctbrand.value == "Other") {
		document.getElementById("ctmodel").style.visibility = "hidden";
		document.getElementById("bother").style.visibility = "visible";
	} else {	
		document.getElementById("ctmodel").style.visibility = "visible";
		document.getElementById("bother").style.visibility = "hidden";
		modeld = document.getElementById('ctmodel');
		removeOptions(modeld);
		sbrand = document.st.ctbrand.value;
		var opt = document.createElement('option');
		opt.value = "";
		opt.innerHTML = "-- Select Model --";
		modeld.appendChild(opt);
		for (var i = 0; i<=nr; i++){
			if (brand[i] == sbrand) {
				var opt = document.createElement('option');
				opt.value = model[i];
				opt.innerHTML = model[i];
				modeld.appendChild(opt);
			}			
		}
		var opt = document.createElement('option');
		opt.value = "Other";
		opt.innerHTML = "Other";
		modeld.appendChild(opt);
	}	
}

Open in new window


When page displays in browser, only the value "Other" appears in the ctmodel select.

Since the value of ctbrand is NOT other, why does this happen?

Thanks
Avatar of Leonidas Dosas
Leonidas Dosas
Flag of Greece image

Richard you have multiple syntactical errors in your code.I debug your javascript code until one line but you must define what are some variables that you are using as I show you below. I post the code as I debug it and the image with red circle variables that you haven't define these yet:
  <!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>	</title>
</head>
<body onLoad="chk_models();">
	<div class="row">
			<div class="col-sm-3 col-xs-3"><div style="padding-left:5px;"><u>Select Brand/Model</u></div></div>
			<div class="col-sm-3 col-xs-3">				
			<select name="ctbrand" id="ctbrand" onChange="get_mods();">
					<option value="">-- Select Brand --</option>
					<option value="BAC" >BAC</option>
					<option value="EVAPCO" selected>EVAPCO</option>
					<option value="SPX/MARLEY" >SPX/MARLEY</option>
					<option value="Other" >Other</option>
				</select>
				
			</div>
			<div class="col-sm-1 col-xs-1">&nbsp;</div>

			<div class="col-sm-3 col-xs-3">				
			<select name="ctmodel" id="ctmodel" onChange="get_dims();">
		<option value="">-- Select Model --</option>	
		</select><br><span style="visibility:hidden;"><input type="text" name="bother" size="14" id="bother"></span>
				
			</div>
		</div>
		<script>
		var ctbrand=document.getElementById('ctbrand');
		  function chk_models() {
	            if (ctbrand.value != "") {
		            get_mods();
	            }
              }
			  
			  
			function get_mods() {
			
	if (ctbrand.value == "Other") {
		document.getElementById("ctmodel").style.visibility = "hidden";
		document.getElementById("bother").style.visibility = "visible";
	} else {	
		document.getElementById("ctmodel").style.visibility = "visible";
		document.getElementById("bother").style.visibility = "hidden";
		modeld = document.getElementById('ctmodel');
		ctmodel.remove();		
		sbrand =ctbrand.value;
		var opt = document.createElement('option');
		opt.value = "";
		opt.innerHTML = "-- Select Model --";
		modeld.appendChild(opt);
		for (var i = 0; i<=nr; i++){
			if (brand[i] == sbrand) {
				var opt = document.createElement('option');
				opt.value = model[i];
				opt.innerHTML = model[i];
				modeld.appendChild(opt);
			}			
		}
		var opt = document.createElement('option');
		opt.value = "Other";
		opt.innerHTML = "Other";
		modeld.appendChild(opt);
	}	
}  
		</script>
</body>
</html>

Open in new window



User generated image
Avatar of Richard Korts

ASKER

Sorry, I forgot to include all the code:

<script type="text/javascript">
window.onload = function () {
   set_pointers();
};
brand = new Array();
model = new Array();
leng = new Array();
wid = new Array();
dpt = new Array();
smodel = "<? print $_SESSION['ctmodel']; ?>";
nr = <? print $nr; ?>;
<? for ($i = 0; $i < $nr; $i++) { 
	$m = mysqli_fetch_array($res,MYSQLI_ASSOC);?>
	brand[<? print $i; ?>] = "<? print $m['Brand']; ?>";
	model[<? print $i; ?>] = "<? print $m['Model']; ?>";
	leng[<? print $i; ?>] = "<? print $m['Length']; ?>";
	wid[<? print $i; ?>] = "<? print $m['Width']; ?>";
	dpt[<? print $i; ?>] = "<? print $m['Depth']; ?>";
<? } ?>	

Open in new window


This code occurs right after (as you can see) the <script type="text/javascript">

If you are not familiar with php, these values are loaded from a database using php.

If necessary, I can show you the source after php translation.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Leonidas Dosas
Leonidas Dosas
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I could not solve it this way, I tried another, it worked.

Points awarded for effort.