Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a Select inside a button?

I am trying to figure out how I could place a select inside a button so that I could use the select to select stuff, but the button outside the select would do other stuff onclick.

This entire white area is button and the select can be seen inside it. I would like to use all the white area as a button and if select is clicked the select would open.

Right now button onclick is triggered if I click select.

Can I create something using jQuery to detect if select is clicked and else button click is triggered?

enter image description here

<button type="button" onclick="doStuff()" class="btn-lg btn-menu col-xs-6">
    Button
    <select>
        <option value="mon">Monday</option>
        <option value="tue">Tuesday</option>
        <option value="wed">Wednesday</option>
    </select>
</button>
like image 389
Firze Avatar asked Nov 18 '14 12:11

Firze


People also ask

How do I add a button inside a select tag?

You can't put a functional button in place of one of the options. The select and option tags create special fields, which you can format in css but not add buttons inside.

How to select an HTML element?

The id selector uses the id attribute of an HTML element to select a specific element. The id of an element is unique within a page, so the id selector is used to select one unique element! To select an element with a specific id, write a hash (#) character, followed by the id of the element.

What do you find select button?

The Find Select buttons of the Editing group provides various tools; which helps to search for the data throughout the Worksheet. Firstly, the Find button helps to search for number, alphabet, texts, special character, spaces etc.


2 Answers

My first solution didn't work cross-browser, because HTML5 doesn't allow interactive elements (such as select) within buttons.

This new solution replaces buttons with spans as Jason van der Zeeuw suggested:

$('span').click(function(event) {
   if(event.target.tagName === 'SPAN') {
       alert('span pressed!');
   }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span>
  Button
   <select>
      <option value="mon">Monday</option>
      <option value="tue">Tuesday</option>
      <option value="wed">Wednesday</option>
   </select>
</span>
like image 118
Rick Hitchcock Avatar answered Sep 27 '22 02:09

Rick Hitchcock


According to the W3C HTML5 Specification, the button element cannot have interactive descendants:

Content model:
Phrasing content, but there must be no interactive content descendant.

Since select is an interactive element, putting a select inside a button is not valid html.

Just out of curiosity, I tried playing around with the z-index property to increase the stack order of the select.

JS Bin

Firefox followed the standard and did not allow interaction on the select.

Of course, you can use another structure for the parent element.

like image 29
Andrew Johnston Avatar answered Sep 27 '22 02:09

Andrew Johnston