Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Append to Select list

Posted on 2013-06-12
2
Medium Priority
?
244 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 34

Assisted Solution

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

Accepted Solution

by:
SANDY_SK earned 750 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
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…
Suggested Courses

876 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