Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I need dropdownlist with parent child combination like below example

Parent 1
 Child of Parent 1
 Child of Parent 1 
Parent 2
 Child of Parent 2
 Child of Parent 2
 Child of Parent 2
 Child of Parent 2

I need to show option like above where both parent and child option can be selected by users.


What I have tried:

I have checked the jquery example but parent can not be selected.I need to select both the options parent and child.

[Demo where we can select child only]
Posted
Updated 20-Sep-16 0:53am
v2
Comments
Karthik_Mahalingam 20-Sep-16 6:29am    
what you have tried apart from the library?
itsathere 20-Sep-16 6:33am    
http://stackoverflow.com/questions/1146789/rendering-a-hierarchy-of-options-in-a-select-tag

Trying to implement the same.
Karthik_Mahalingam 20-Sep-16 6:55am    
chk my solution.

1 solution

check this

<!DOCTYPE html>
<html>
<head>
    <title></title>    

</head>
<body>
    <select id="ddl"></select>

    <script>

        var json = [
            { parent: '1', item: 'Electronics' },
            { parent: '0', item: 'Play station' },
            { parent: '0', item: 'audios' },
            { parent: '1', item: 'Books' },
            { parent: '0', item: 'Non-Fiction' },
            { parent: '0', item: 'Maths' },
        ];
        options = [];
        var ddl = document.getElementById('ddl');
        for (var i = 0; i < json.length; i++) {

            var style = '', space = '&nbsp;&nbsp;'
            if (json[i].parent == '1')
            {
                style = "style='font-weight:bold'"
               space =''

            }
            var html = "<option " + style + "  > " + space + json[i].item; +" </option>"

            options.push(html);
        }

        ddl.innerHTML = options.join('\n'); 
    </script>
</body>
</html>


demo: Edit fiddle - JSFiddle[^]
 
Share this answer
 
v2
Comments
itsathere 20-Sep-16 8:12am    
It's same solution I have tried and given link of stackoverflow, have few modifications that's it. btw thanks.
Karthik_Mahalingam 20-Sep-16 9:00am    
welcome, if you need in some other way, let me know
itsathere 20-Sep-16 9:27am    
thanks, I have done it by myself before you have provided the solution.
Karthik_Mahalingam 20-Sep-16 9:28am    
oh, good.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900