1

I want to create a multi level drop down box. when I click on setauthor it should display a submenu with some names

when I click on setauthor it should display a submenu with some names

My first level code :

<select>
     <option value="Like">Like</option>
     <option value="Star">Star</option>
     <option value="Set Author">Set Author</option>
</select>

Kindly help me.

Micho
  • 3,929
  • 13
  • 37
  • 40
Nandhini
  • 11
  • 1
  • 2

3 Answers3

1

Try this

<select>
 <optgroup label="First Group">
      <option value="Like">Like</option>
      <option value="Star">Star</option>
 </optgroup>
 <optgroup label="Second Group">     
      <option value="Set Author">Set Author</option>
 </optgroup>
</select>
Ari Singh
  • 1,228
  • 7
  • 12
  • Thanks for your answer....when I click on setAuthor it should display a submenu with some names like nandhini,abinaya,.. – Nandhini Feb 25 '18 at 05:55
1

TO achieve expected result, use below option of using eventListener on dropdown

HTML:

<select class="first">
     <option value="Like">Like</option>
     <option value="Star">Star</option>
     <option value="Set Author">Set Author</option>
</select>
<select id="set">
     <option value="John">John</option>
     <option value="Doe">Doe</option>
     <option value="Mark">Mark</option>
</select>
<select id="star">
     <option value="one">1</option>
     <option value="two">2</option>
     <option value="three">3</option>
     <option value="four">4</option>
     <option value="five">5</option>
</select>
<select id="like">
     <option value="like">like</option>
     <option value="good">good</option>
     <option value="bad">bad</option>
</select>

CSS:

#set, #star{
  display:none
}

JS:

var elem = document.querySelectorAll('.first')

for(var i =0; i < elem.length; i++){
  elem[i].addEventListener('change',function(e){
    if(e.target.value == 'Set Author'){
       document.getElementById('set').style.display = 'inline-block'
       document.getElementById('star').style.display = 'none'
       document.getElementById('like').style.display = 'none'
    }
    if(e.target.value == 'Star'){
       document.getElementById('star').style.display = 'inline-block'
       document.getElementById('set').style.display = 'none'
       document.getElementById('like').style.display = 'none'
    }
    if(e.target.value == 'Like'){
       document.getElementById('like').style.display = 'inline-block'
       document.getElementById('star').style.display = 'none'
       document.getElementById('set').style.display = 'none'
    }
  })
}

codepen- https://codepen.io/nagasai/pen/rJqVje

Naga Sai A
  • 10,771
  • 1
  • 21
  • 40
0

You can use lists as shown below. Then use bootstrap to style the menu.

    <select>
         <ul>
         <li>Like</li>
         <li>Star</li>
           <ul>
              <li>Set Author</li>
           </ul>
         </ul>
    </select>
Abiud Orina
  • 1,162
  • 8
  • 19