0

I need to add close button to clear select element in select tag, as well as I need to make the dropdown like a button as below image enter image description here

Here is my HTML code:

<div class="form-group">
  <label for="sel1">Select Asset:</label>
  <select class="form-control" id="assetCategory">
      <option value="" selected disabled>Select Asset</option>
      <option value="1">Asset 1</option>
      <option value="2">Asset 2</option>
      <option value="3">Asset 3</option>
      <option value="4">Asset 4</option>
  </select>
</div> 

How can I do this? Or is it not possible to do?

bfontaine
  • 18,169
  • 13
  • 73
  • 107
Udara Herath
  • 805
  • 2
  • 18
  • 37

4 Answers4

3

You have to style the select tag the way it looks on your image. The cancel button must be a seperat HTML Tag, which than have to be styled on its own and positioned next to the select.

Your html would look something like this:

<select>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>
<button>X</button>
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
Niew View
  • 66
  • 5
2

You can do it with ease using bootstrap and JQuery. However the logic remains the same if you choose something else. Check the snippet below:

$("#clear_addon").on("click", function(){
  $("#target_select").val("");
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="input-group">
  <select class="form-control" id="target_select">
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
  </select>
  <span class="input-group-addon" id="clear_addon">Clear</span>
</div>
Oleh Rybalchenko
  • 6,998
  • 3
  • 22
  • 36
2

Try something like following:

$("#clear_addon").on("click", function(){
  $("select").val("");
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="input-group">
  <select class="form-control"  aria-describedby="clear-addon">
  <option></option>
  <option>option1</option>
  <option>option2</option>
  </select>
  <span class="input-group-addon" id="clear_addon">&#10006;</span>
</div>
Govind Samrow
  • 9,981
  • 13
  • 53
  • 90
1

some ugly but it might give a idea look

$("button").click(function(){
 $("#a").val('');
});
select {
  width: 200px;
  border: 1px solid #ccc;
}

button {
  position:fixed;
  left:158px;
  padding: 0px;
  width: 50px;
  background-color:white;
  border: 1px solid #ccc;
}
div select, button{
  height:20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <select id="a">
    <option value="a">a</option>
    <option value="c">b</option>
    <option value="c">c</option>
  </select>
  <button>remove</button>
</div>