Click to See Complete Forum and Search --> : New to JavaScript, simple question


Seth
04-18-2003, 08:10 AM
Hi everybody, I have a simple problem. I need a Javascript that will write the value of a drop-down menu to the page, but the text needs to be formatted with HTML. Can anybody help me?

Thanks

pyro
04-18-2003, 08:31 AM
Try something like this:

<html>
<head>
<script language="javascript" type="text/javascript">
function addText(frm)
{
document.getElementById("mydiv").innerHTML = "<span style='color:darkblue;font-weight:bold;'>"+frm.options[frm.selectedIndex].value+"</span>";
}
</script>
</head>
<body>
<form name="myform">
<select name="menu" onChange="addText(this)">
<option value="one">one</option>
<option value="two">two</option>
</select>
</form>
<div id="mydiv"></div>
</body>
</html>

Seth
04-18-2003, 08:41 AM
Great, thanks. How would you make the default selected value appear automatically when the page loads (without selecting anything)?

pyro
04-18-2003, 08:45 AM
Try this:

<html>
<head>
<script language="javascript" type="text/javascript">
function addText()
{
frm = document.myform.menu;
document.getElementById("mydiv").innerHTML = "<span style='color:darkblue;font-weight:bold;'>"+frm.options[frm.selectedIndex].value+"</span>";
}
</script>
</head>
<body onLoad="addText()">
<form name="myform">
<select name="menu" onChange="addText()">
<option value="one">one</option>
<option value="two">two</option>
</select>
</form>
<div id="mydiv"></div>
</body>
</html>

Seth
04-18-2003, 09:04 AM
The onLoad thing didn't work, but I think it might be a problem with other scripts on my page.
Thanks for helping!

Seth
04-18-2003, 09:09 AM
it works when it's just your code and nothing else, but i have to figure out what's causing problems on my page.

thanks again