javascript - Bootstrap dropdown as select option -
how make bootstrap dropdown act html select option? had problem. partially think bootstrap missing feature in core library, team must have reason not including in library.
this first time posting q&a question. if doesn't question, i'm sorry.
here extended methods:
$.fn.extend({ dropdownconvert: function(b) { b = b || true; console.log(b); this.each(function(){ var $this = $(this); if ($this.prop("tagname") == "select" && typeof $this.attr("id") == "string" && $this.find("option").length > 0) { var temp = '<div class="dropdown" id="' + $this.attr("id") + '"><button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"><span class="selected">' + $this.find("option")[0].innerhtml + '</span> <span class="caret"></span></button><ul class="dropdown-menu">'; var o = $this.find("option"); (var = 0; < o.length; i++) { temp += '<li><a>' + o[i].innerhtml + '</a></li>'; } temp += '</ul></div>'; $this[0].outerhtml = temp; if (b) $("#"+$this.attr("id")).dropdown(); } }); }, dropdown: function(cb) { console.log($(this)); this.each(function(){ var $drop = $(this); $drop.find("a").bind("click", function(){ var selected = $(this).text(); $drop.find(".selected").text(selected); $drop.attr("data-selected", selected.tolowercase()); if (typeof cb === "function") { cb.call($drop, selected.tolowercase()); } }) }); return this; }, dropdownval: function(v) { var $drop = $(this).first(); if($drop.hasclass("dropdown")) { if (v !== undefined) { v = string(v); var c = string(v+" ")[0].touppercase() + v.substring(1,v.length); $drop.attr("data-selected", v); $drop.find(".selected").text(c); } else { if ($drop.data("selected") !== undefined) { return $drop.attr("data-selected"); } else { return $drop.find(".selected").text(); } } } else { return undefined; } } });
$(elem).dropdownconvert();
this convert <select>
<option>
elements bootstrap dropdown. the element must have id
else function not run properly.
arguments: b
: boolean, default - true. set false stop elements attaching dropdown click event. if unset, code run $(elem).dropdown()
after conversion.
$(elem).dropdown();
this make bootstrap downdowns behave <select>
<option>
elements. button text change last item you've clicked.
arguments: cb
: function. callback function. called argument cb()
selected item.
$(elem).dropdownval();
this similar $(elem).val()
input elements. selected item return. $(elem).dropdown()
needs run prior calling method.
arguments: value
: converted text. if argument other undefined
, set current selected option value provided regardless of whether 1 of default options in select. leaving blank return selected value.
auto convert bootstrap-styled dropdown when page loads
$(function(){ $("select").dropdownconvert(); })
demo
$.fn.extend({ dropdownconvert: function(b) { b = b || true; console.log(b); this.each(function(){ var $this = $(this); if ($this.prop("tagname") == "select" && typeof $this.attr("id") == "string" && $this.find("option").length > 0) { var temp = '<div class="dropdown" id="' + $this.attr("id") + '"><button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"><span class="selected">' + $this.find("option")[0].innerhtml + '</span> <span class="caret"></span></button><ul class="dropdown-menu">'; var o = $this.find("option"); (var = 0; < o.length; i++) { temp += '<li><a>' + o[i].innerhtml + '</a></li>'; } temp += '</ul></div>'; $this[0].outerhtml = temp; if (b) $("#"+$this.attr("id")).dropdown(); } }); }, dropdown: function(cb) { console.log($(this)); this.each(function(){ var $drop = $(this); $drop.find("a").bind("click", function(){ var selected = $(this).text(); $drop.find(".selected").text(selected); $drop.attr("data-selected", selected.tolowercase()); if (typeof cb === "function") { cb.call($drop, selected.tolowercase()); } }) }); return this; }, dropdownval: function(v) { var $drop = $(this).first(); if($drop.hasclass("dropdown")) { if (v !== undefined) { v = string(v); var c = string(v+" ")[0].touppercase() + v.substring(1,v.length); $drop.attr("data-selected", v); $drop.find(".selected").text(c); } else { if ($drop.data("selected") !== undefined) { return $drop.attr("data-selected"); } else { return $drop.find(".selected").text(); } } } else { return undefined; } } }); $(function(){ $("select").dropdownconvert(); $("#t-1").click(function(){ $("#result").text($("#v-1").dropdownval()); }); $("#t-2").click(function(){ $("#v-1").dropdownval("melon"); }); })
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> <h1>this select option:</h1> <select id="v-1"> <option>apple</option> <option>orange</option> <option>banana</option> </select> <button type="button" id="t-1">what selected option?</button> <button type="button" id="t-2">set value "melon"</button> <p id="result"></p>
Comments
Post a Comment