Javascript help needed please

Soldato
Joined
11 Feb 2004
Posts
4,534
Location
Surrey, UK
I have some semicolon seperated values stored in a SQL database field as follows:
AA99;BB99;CC99;DD99

I want these values to show as selectable individual options in a drop down list in a web page. So they appear as:
AA99
BB99
CC99
DD99

Is this possible? I've tried Googling for the JScript but no search criteria seems to find a match which meets the above.

The value seperator can be changed it needed.
 
Soldato
Joined
9 May 2005
Posts
4,524
Location
Nottingham
Here is one way of doing it:

Code:
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>JavaScript Select Test</title>
	</head>
	<body>
	
		<form action="" method="">
			<select id="select_01">
			</select>
		</form>
		
		<script type="text/javascript">
			var str = "[COLOR="Red"][B]AA99;BB99;CC99;DD99[/B][/COLOR]";
			var tokens = str.split(";");
			
			var select = document.getElementById("select_01");

			for (var i in tokens)
			{
				var opt = document.createElement("option");
				var text = document.createTextNode(tokens[i]);
				
				opt.setAttribute("value",tokens[i]);
				opt.appendChild(text);
				
				select.appendChild(opt);		
			} 
		</script>
	
	</body>
</html>
 
Last edited:
Top Bottom