Solved

Append to Select list

Posted on 2013-06-12
2
231 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Getting selected value using Jquery 3 42
AJAx/JQuery/JSON Error 3 38
How do I wrap text in dialog window 6 34
jquery change div image on page reload using Jquery 9 57
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
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…

732 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