Solved

Append to Select list

Posted on 2013-06-12
2
225 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 32

Assisted Solution

by:Big Monty
Big Monty earned 250 total points
Comment Utility
0
 
LVL 11

Accepted Solution

by:
SANDY_SK earned 250 total points
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now