Solved

Append to Select list

Posted on 2013-06-12
2
229 Views
Last Modified: 2013-11-06
Hi,

I want to append items to a select list including the option group how would I do this with the sample data below.

Group Fruit
           Apple
           Orange
           Banana
Group Veg
           Pea
           Cabbage
           Potato
0
Comment
Question by:JoeBo747
2 Comments
 
LVL 33

Assisted Solution

by:Big Monty
Big Monty earned 250 total points
ID: 39242176
0
 
LVL 11

Accepted Solution

by:
SANDY_SK earned 250 total points
ID: 39309621
Something like this

<html>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script>
jQuery(document).ready(function(){

	$('<optGroup/>').attr('label', 'Group Fruit').appendTo($("#sampleSel"));
	$("#sampleSel").find('optGroup').last().append('<option value=11>Apple</option>');
	$("#sampleSel").find('optGroup').last().append('<option value=12>Orange</option>');
	$("#sampleSel").find('optGroup').last().append('<option value=13>Banana</option>');
	$('<optGroup/>').attr('label', 'Group Veg').appendTo($("#sampleSel"));
	$("#sampleSel").find('optGroup').last().append('<option value=21>Pea</option>');
	$("#sampleSel").find('optGroup').last().append('<option value=22>Cabbage</option>');
	$("#sampleSel").find('optGroup').last().append('<option value=23>Potato</option>');
});

</script>

<select id='sampleSel'>
</select>
</html>

Open in new window

0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question