/    Sign up×
Community /Pin to ProfileBookmark

Simulate onChange event?

Hi ther i have a function that creates a drop down based on the value of another drop down. These are named cat and subcat.

The arrays i use are generated by asp wich pulls the info from a sql db.

Everything works fine.

The script is used to categorize products in the DB.
My prolem occurs when i want to edit these records and not change the category.

I use asp to select the category by matching the item value against the one from the db.

I need to then populate the subCat drop down and select the appropriate subcat. The script to populate is normaly fired from the onChange event of the cat dropdown.

Is there any way i can simulate the onchange event for the cat drop down when the page loads?

or can someone suggest an alternative?

below is the code used to create the arrays

[CODE]
// create arrays on the fly from the DB
<%
Set oConn2 = Server.CreateObject(“ADODB.Connection”)
OConn2.ConnectionTimeout = 20
Set catSet = Server.CreateObject(“ADODB.RecordSet”)
Set getSubCat = Server.CreateObject(“ADODB.RecordSet”)
oConn2.Open connstring

sql = “SELECT Count(*) AS recTotal FROM cattypetbl”

set catSet = oConn.execute(sql)
recTotal = catSet(“recTotal”)
%>

var store = new Array();

<%

catCount = 1
do while catCount <= recTotal
%>

store[<% = (catCount) %>] = new Array(
<%
catSql = “select * from SubCatTypeTbl where CatTypeLink =” + cstr(catCount)
set getSubCat = oConn.execute(catSql)
do while not getSubCat.eof
%>
‘<% = getSubCat(“Descrip”) %>’,'<% = getSubCat(“SubCatTypeLink”) %>’
<% getSubCat.movenext
if not getSubCat.eof then response.write(“,”)
loop
%>);
<%
catCount = catCount + 1
loop
set getSubCat = nothing
%>
// create arrays on the fly from the DB
[/CODE]

and here is the code to populate the drop downs

[CODE]
// scripts to populate drop downs
function init()
{
optionTest = true;
lgth = document.forms[0].subCat.options.length – 1;
document.forms[0].subCat.options[lgth] = null;
if (document.forms[0].subCat.options[lgth]) optionTest = false;
}

function populate()
{
if (!optionTest) return;
var box = document.forms[0].Cat;
var number = box.options[box.selectedIndex].value;
if (!number) return;
var list = store[number];
var box2 = document.forms[0].subCat;
box2.options.length = 0;
for(i=0;i<list.length;i+=2)
{
box2.options[i/2] = new Option(list[i],list[i+1]);
}
}
[/CODE]

I have tried calling the populate function and passing it the category id .
I have also created a cutdown version of the populate function to see if this helped.

[CODE]
function popu()
}
var list = store[number];
var box2 = document.forms[0].subCat;
box2.options.length = 0;
for(i=0;i<list.length;i+=2)
{
box2.options[i/2] = new Option(list[i],list[i+1]);
}
}
[/CODE]

Thanks in advance

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@KorMar 10.2006 — 
Is there any way i can simulate the onchange event for the cat drop down when the page loads?
[/quote]

If you generate the options using a server-side application, simply instruct your application to give the desired option an attribute [B]selected="true"[/B]
Copy linkTweet thisAlerts:
@sidcomauthorMar 10.2006 — Thats what i do on the category drop down.

This selects the correct category but dosnt fire the javascript to populate the subcat drop down.

here is the html and asp for the menus.

[CODE]
<select name="Cat" width=200 id="cat1" onChange="populate()">
<%
sql = "select * from cattypetbl"
set catSet = oConn.execute(sql)
do while not catSet.eof
catTypeLink = catSet("CatTypeLink")
Descrip = catSet("Descrip")
%>


<option value="<% = (catTypeLink) %>" <% if catTypeLink = cat then
catPass = catTypeLink
response.write("selected")
end if
%>><% = (Descrip) %></option>
<%
catSet.movenext
loop
%></select>

<select name="subCat">
<option>No category selected</option>
<option selected>Your browser can't handle this script</option>
</select>
[/CODE]
Copy linkTweet thisAlerts:
@KorMar 10.2006 — aha, then simply fire the function onload

window.onload = populate;
Copy linkTweet thisAlerts:
@sidcomauthorMar 10.2006 — Excellent got that working now needed to remove the compatibility check to get it to fire correctly.

Works a treat now!

Thanks very much ?
×

Success!

Help @sidcom 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.9,
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,
)...