Boostrap Select - Javascript Selectbox Question

Hi Experts,

I am using Bootstrap Select Library for my project to replace my select boxes.  I needed the feature to add custom text into it as well so that user should be able submit text which is not a option. While searching on web, I landed on http://jsfiddle.net/2wvc3Lj5/8/ < this fiddle which allows you to do that.

Now my main issue is that I have alot of dynamic selectbox and it will not be possible to put it the code for each and every selectbox. Throughout my website, my selectbox's have one common class (combobox). Is it possible to make it work with the class name and also if there are multiple selectbox per page, it should change add the value to the selectbox it was added on.

Thanks
Rahul SehrawatAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Here is an attempt at it
CSS (from fiddle)
<style type="text/css">
.dropdown-menu {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

.dropdown-menu a {
   overflow: hidden;
   outline: none;
}

.additem input
{
   border:0;
   margin:-3px;
   padding:3px;
   outline: none;
   color: #000;
   width: 99%;
}

.additem:hover input
{
   background-color: #f5f5f5;
}

.additem .check-mark
{
   opacity: 0;
   z-index: -1000;
}

.addnewicon {
   position: relative;
   padding: 4px;
   margin: -8px;
   padding-right: 50px;
   margin-right: -50px;
   color: #aaa;
}

.addnewicon:hover {
   color: #222;
}
</style>

Open in new window

HTML - this shows two select boxes that are standard bootstrap selects - the code in the jQuery section will add the Add option to all selectpicker's on the page - it is a trivial change to separate out only those selectpickers that must have addoptions
    <div class="row">
      <div class="col-lg-6">
        <h2>Select Sample #1</h2>
        <select class="selectpicker" id="select1">
          <option>Mustard</option>
          <option>Ketchup</option>
          <option>Relish</option>
        </select>    
      </div>
      <div class="col-lg-6">
        <h2>Select Sample #2</h2>
        <select class="selectpicker" id="select2">
          <option>Banana</option>
          <option>Peach</option>
          <option>Orange</option>
        </select>    
      </div>
    </div>

Open in new window

jQuery (functions taken from fiddle and modified to be dynamic). The solution is to find the closet item with a .bootstrap-select class and then select the .prev() item.
<script>
$(function() {
  var content = "<input type=text onKeyDown='event.stopPropagation();' onKeyPress='addSelectInpKeyPress(this,event)' onClick='event.stopPropagation()' placeholder='Add item'> <span class='glyphicon glyphicon-plus addnewicon' onClick='addSelectItem(this,event,1);'></span>";

  var divider = $('<option/>')
          .addClass('divider')
          .data('divider', true);
          

  var addoption = $('<option/>')
          .addClass('additem')
          .data('content', content)
		  
  $('.selectpicker')
          .append(divider)
          .append(addoption)
          .selectpicker();

});

function addSelectItem(t,ev)
{
   ev.stopPropagation();

   var txt=$(t).prev().val().replace(/[|]/g,"");
   if ($.trim(txt)=='') return;
   var p=$(t).closest('.bootstrap-select').prev();
   var o=$('option', p).eq(-2);
   o.before( $("<option>", { "selected": true, "text": txt}) );
   p.selectpicker('refresh');
}

function addSelectInpKeyPress(t,ev)
{
   ev.stopPropagation();

   // do not allow pipe character
   if (ev.which==124) ev.preventDefault();

   // enter character adds the option
   if (ev.which==13)
   {
      ev.preventDefault();
      addSelectItem($(t).next(),ev);
   }
}
</script>

Open in new window


Working sample here

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rahul SehrawatAuthor Commented:
Awesome. Thanks :) works like a charm.
Julian HansenCommented:
You are welcome.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.