/    Sign up×
Community /Pin to ProfileBookmark

How to insert html into javascript menu

Hi Guys

I want html to be inserted into my javascripts so that when the dropdown menu gets “setup” then it insert html code also. If it is possible I don’t know
Below is my javascript menu

[code]
<html>
<head>
<script type=”text/javascript”>
////////////////////////////////////////////////////////////////////////////////////////
// Cities
////////////////////////////////////////////////////////////////////////////////////////
// Name,Line1,Line2,Line3,Line4,Line5,Dell,Tel,FAX,FaxEmail,Email,Webpage
var CityCourierCost = [
[‘Please select your City’,”,”],
[‘Cape Town’,’200′,’Cape Town’],
[‘JHB’,’600′,’JHB’]
//[‘Witbank’,’amount’]
];
function SetupCity(TA) {
var str = “<select id=’City’ onchange=’CityInfo()’>”;
for (var i=0; i<CityCourierCost.length; i++) {
str += ‘<option value=”‘+CityCourierCost[i].join(‘|’)+'”>’+CityCourierCost[i][0]+'</option>’;
}
str +='</select>’;
document.write(str);
}

function CityInfo() {
var sel = document.getElementById(‘City’).selectedIndex;
var tmp = []; tmp.push(sel);
for (var i=1; i<3; i++) { tmp.push(CityCourierCost[sel][i]); }
document.getElementById(‘CourierCost’).value = tmp[1];
document.getElementById(‘CityName’).value = tmp[2];

}
</script>
</head>
<body >
<form name=”theform” action=”” method=”post”>
<table width=”863″ height=”53″>
<tr>
<td width=”90″ height=”24″>City</td>
<td width=”193″><script type=”text/javascript”>SetupCity();</script></td>
<td width=”183″>City Selected:</td>
<td width=”377″><input type=”text” name=”CityName” id=”CityName” size=”40″ value=”” ></td>
</tr>
<tr>
<td height=”21″>&nbsp;</td>
<td>&nbsp;</td>
<td>Courier Cost</td>
<td><input type=”text” name=”CityCourierCost” id=”CourierCost” size=”40″ value=”” onBlur=”startCalc();” readonly ></td>
</tr>
</table>
</form>
</body>
</html>

[/code]

You see my dropdown menu gets populated with an array but as there are many items in the array (not shown in example) I want to split them with an optgroup. the html version of an optgroup looks like this

[code]
<select name=”Other City”>
<optgroup label=”Mpumalanga”>
<option value=”MP – boekenhouthoek”>boekenhouthoek</option>
<option value=”MP – carolina”>carolina</option>
<option value=”MP – embalenhle”>embalenhle</option>
<option value=”MP – emzinoni”>emzinoni</option>
<option value=”MP – ermelo”>ermelo</option>
<option value=”MP – jeppe’s reef”>jeppe’s reef</option>
</optgroup>

<optgroup label=”Mpumalanga”>
<option value=”MP – nelspruit”>nelspruit</option>
<option value=”MP – sakhile”>sakhile</option>
<option value=”MP – secunda”>secunda</option>
<option value=”MP – siyabuswa”>siyabuswa</option>
<option value=”MP – wesselton”>wesselton</option>
<option value=”MP – witbank”>witbank</option>
</optgroup>
</select>
[/code]

so my problem is to make my javascript menu have optgroups
Is this possible as there are over 300 items and I want to group them
Any help would be appriaciated

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@haulinMar 30.2009 — you can write it just like you are writing the city [B]option[/B] tags - with [B]document.write[/B] (although there are better DOM methods available), but you need to tell the script somehow when to create the [B]optgroup[/B] tags. for example change your array to sth like this:
[CODE]
var CityCourierCost = [
['Please select your City','',''],
['optgroup','Mpumalanga1',''],
['Cape Town','200','Cape Town'],
['optgroup','Mpumalanga2',''],
['JHB','600','JHB']];
[/CODE]

now we know that whenever the [B]CityCourierCost[i][0]==='optgroup'[/B], we need to create a new [B]optgroup[/B] labeled [B]CityCourierCost[i][1][/B] instad of creating a city [B]option[/B]. got it? ?
×

Success!

Help @JPM 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.19,
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,
)...