0

i am using a semantic ui dropdown with the following code

$('.ui.dropdown')
  .dropdown()
;
<html>
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.css" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.js"></script>
  </head>
  <body>

    <select class="ui dropdown">
      <option value="">Select option</option>
      <option value="1">Option 1</option>
      <option value="2">Option 2</option>
      <option value="3">Option 3</option>
      <option value="4">Option 4</option>
      <option value="5">Option 5</option>
      <option value="6">Option 6</option>
      <option value="new">+Add new</option>
    </select>

</body>
</html>

I am not able to give fixed position to the last option. I want the last option '+Add more' to have fixed position and should not get scrolled along with other options. I want the add new option to be like how it is in this image

2 Answers2

1

You can define it like this with the css property sticky:

$('#sticky').bind('click', function(event) {
  //do what you want
  var x = document.getElementById("industries_served");
  var option = document.createElement("option");
  option.text = prompt("Please enter a new value", "Florida");
  x.add(option);
});
* {
  font-size: 16px;
}

#sticky {
  position: -webkit-sticky;
  /* Safari */
  position: sticky;
  bottom: 0;
  border-top: .5px solid;
  color: blue;
}

select {
  border: none;
}

#wrapper {
  border: 1px solid;
  width: fit-content;
  margin: auto;
  /* just here to center the div*/
}

.dropbtn {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  border: none;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
  z-index: 1;
}

#industries_served option:hover {
  background-color: #ddd;
}

#sticky:hover {
  background-color: #ddd;
}

.dropdown:hover .dropdown-content {
  display: block;
}

.dropdown:hover .dropbtn {
  background-color: #3e8e41;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="dropdown">
  <button class="dropbtn">Dropdown</button>
  <div class="dropdown-content">
    <div id="wrapper">
      <select class="ui fluid search dropdown" id="industries_served" name="industries_served" multiple="">
        <option value="">State</option>
        <option value="AL">Alabama</option>
        <option value="AK">Alaska</option>
        <option value="AZ">Arizona</option>
        <option value="AR">Arkansas</option>
        <option value="AR">New Mexico</option>
        <option value="AR">New York</option>
        <option value="AR">???</option>
      </select>
      <div id="sticky">+ Add more</div>
    </div>
  </div>
</div>
Stephan T.
  • 5,843
  • 3
  • 20
  • 42
  • Thanks for the response but this is not what i want. I want the add more option to be inside the dropdown and have fixed position. With your code it is not inside the dropdown and is always visible. I am using the dropdown with HTML elements so that i can style them – Rishab Saklani Dec 13 '18 at 07:24
  • I strongly disagree, my answer looks exactly like your desired output... – Stephan T. Dec 13 '18 at 07:27
  • Please check the image again. Your add more button is always visible. It should only be visible when somebody opens the dropdown and should appear as one more option. – Rishab Saklani Dec 13 '18 at 07:34
  • I edited my answer, but the add more button is NOT another option in that image :D – Stephan T. Dec 13 '18 at 07:42
  • Thanks for the response. I have updated my code and included semantic library also. Would it be possible for you to make add new option fixed in my updated code – Rishab Saklani Dec 13 '18 at 07:55
0

pls try this code :

<!DOCTYPE html> 
<html>

<head>
<meta charset="UTF-8">
<style>

    select {
        position: relative;
    }
    select option {
        position: relative;
    }
    select option:last-child {
        position: sticky;
        bottom: 0;
        background-color: red;
    }
    </style>
 </head>
 <body>
    <select class="ui fluid search dropdown" id="industries_served" name="industries_served" multiple="">
        <option value="">State</option>
        <option value="AL">Alabama</option>
        <option value="AK">Blaska</option>
        <option value="AZ">CArizona</option>
        <option value="AR">DArkansas</option>
        <option value="AR">+Add more</option>
    </select>

    </body>

   </html>
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57