So - jquery autocomplete and extra div with special character icons that can be inserted into a search box (such as É, ã etc)...
Whenever user types - autocomplete dropdown show up, but when user needs to click on a special character icon (so it would be inserted into a searchbox) -> autocomplete dropdown predictably closes as focus has been lost:(
Is there a way to prevent jquery autocomplete dropdown from hiding on lost focus in cases when user clicks on one of the special character icons?
EDIT: This is directed towards the jquery autocomplete combobox for use with selectbox, but the same solution and thought process definitely applies for autocomplete as well (I was working on the combobox when I came across your problem :)
HTML:
<select class="combobox">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="add_another">Add Another Option</option>
</select>
CSS:
.force-open{
display:block !important;
}
JS:
$(function() {
$( ".combobox" ).combobox({
// the select event is apparently the only event
// that you can pass a function, so if your goal
// is to keep it open specifically when a user
// clicks an option, then this is where you should start
select: function(event, ui) {
$(event.currentTarget)
.parent()
.next("ul")
.addClass("force-open");
}
});
$(".custom-combobox .ui-button").click(function(){
//This is the actual answer to your question,
//which is specifically forcing the jquery selectbox
//to stay open after it has already been opened,
//regardless of the change in focus.
$(this)
.parent()
.next("ul")
.addClass("force-open");
})
});
Explanation:
Normally what I try to do if there are no good exposed events for a given library or chunk of code when doing web development (other than try to hack at/update the source code...) is look at the structure of html that is generated from the library/js (if there is any at all).
In this case, the generated html from jquery-ui after calling $( ".combobox" ).combobox({...}) is:
<select class="combobox" style="display: none;">
....shown above in HTML section...
<span class="custom-combobox">
<span class="ui-helper-hidden-accessible" role="status" aria-live="polite">4 results are available, use up and down arrow keys to navigate.</span>
<input class="custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left ui-autocomplete-input" title="" autocomplete="off">
<a class="ui-button ui-widget ui-state-default ui-button-icon-only custom-combobox-toggle ui-corner-right" tabindex="-1" title="Show All Items" role="button" aria-disabled="false">
</span>
<ul id="ui-id-1" class="ui-autocomplete ui-front ui-menu ui-widget ui-widget-content ui-corner-all" tabindex="0" style="display: none; width: 209px; top: 43px; left: 8px;">
<li class="ui-menu-item" role="presentation">
<a id="ui-id-2" class="ui-corner-all" tabindex="-1">Volvo</a>
</li>
<li class="ui-menu-item" role="presentation">
<a id="ui-id-3" class="ui-corner-all" tabindex="-1">Saab</a>
</li>
<li class="ui-menu-item" role="presentation">
<a id="ui-id-4" class="ui-corner-all" tabindex="-1">Mercedes</a>
</li>
<li class="ui-menu-item" role="presentation">
<a id="ui-id-5" class="ui-corner-all" tabindex="-1">Add Another Option</a>
</li>
</ul>
This gives me a great jumping-off point in order to add/remove css classes in order to force the functionality I want. Remember, this is a last resort, and should only be abused when there isn't a fully featured api to help bind/unbind events. By examining the html structure, I can use jquery selectors/methods to get at the correct 'ul' tag I am trying to keep open, in this case. So after I grab the ul I want using jquery, I addClass 'force-open' which has 'display:block !important;' , ultimately forcing the jquery combobox to stay open, regardless of focus. Now simply removeClass 'force-open' when you feel it is time to 'shut' the combobox.
Hope this helps :).
Related
I have some html
<select id="dropd" class="search-type" data-toggle="dropdown">
<option value="">All</option>
<option value="standards-and-publications">Standards</option>
<option value="sedl-digital-library">Library</option>
</select>
and this small js function which I am using to attempt to select this drop down menu and apply a key listener to. However I am having trouble getting this code below
$('.search-type').on('show.bs.dropdown', function () {
alert("I FINALLY FOUND YOU");
});
to run when the drop down is shown (clicked in this case). Do I need to wrap in this in an event listener listening for clicks? I have actually already tried this method but still could not get it to display this message or react in any way when the user clicked on the drop down. Based on what I have seen when I ran through this in the debugger, It never resolves the class selector, but this is just a guess. Is there something glaringly obvious I am missing or some weird bootstrap rule when using selectors?
Also I apologize if I am not giving enough info I am relatively new to this and am unsure of what is going on entirely.
See here for an answer: Process for using show.bs.dropdown in Bootstrap
Basically, the event is handled by the parent of the drop-down, not the drop-down itself. Also, BootStrap dropdowns are usually <ul>s. Here's an example of a standard drop-down with a handler:
HTML
<div class="btn-group" id="ddlWrap">
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
Menu
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li>Choice1</li>
<li>Choice2</li>
<li>Choice3</li>
<li class="divider"></li>
<li>Choice..</li>
</ul>
</div>
JS
$('ddlWrap').on('show.bs.dropdown', function () {
alert('Found the dropdown wrapper!');
});
I think the events fire on the parent.So it should be the element above the .dropdown.So your code should be
<div class="btn-group" id="myDropdown">
<select id="dropd" class="search-type" data-toggle="dropdown">
<option value="">All</option>
<option value="standards-and-publications">Standards</option>
<option value="sedl-digital-library">Library</option>
</select>
</div>
I have two dropdowns:
First Dropdown:
First Dropdown Code:
<select name="idx" id="masthead_search">
<option value="">Library catalog</option>
<option value="ti">Title</option>
<option value="au">Author</option>
<option value="su">Subject</option>
<option value="nb">ISBN</option>
<option value="se">Series</option>
<option value="callnum">Call number</option>
</select>
Second Dropdown:
Second Dropdown Code:
<select name="branch_group_limit" id="select_library">
<option value="">All libraries</option>
<option value="branch:01">Main Library</option>
</select>
In order to change the highlight background of the option tag when hovered, I converted select and option tag to ul li tag using this jquery code.
/** Convert a select-option tag to ul-li tag to change its dropdown highlight color from blue to green **/
$(document).ready(function(){
/**masthead_search **/
$('#masthead_search').parent().prepend('<div class="btn-group btn-input clearfix"> <button type="button" class="btn btn-default dropdown-toggle form-control" data-toggle="dropdown"><span data-bind="label">Library catalog</span> <span class="caret"></span></button><ul id="newmasthead_search" name="masthead_search" class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">');
$('#masthead_search option').each(function(){
$('#newmasthead_search').append('<li value="' + $(this).val() + '" role ="presentation"><a tabindex="-1" role="menuitem">'+$(this).text()+'</a></li>');
});
$('#newmasthead_search').append('</ul></div>');
$('#masthead_search').remove();
$('#newmasthead_search').attr('id', 'masthead_search');
/**select_library **/
$('#select_library').parent().prepend('<div class="btn-group btn-input clearfix"><button type="button" class="btn btn-default dropdown-toggle form-control" data-toggle="dropdown"><span data-bind="label">All libraries</span> <span class="caret"></span></button><ul id="newselect_library" name="branch_group_limit" class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu2">');
$('#select_library option').each(function(){
$('#newselect_library').append('<li value="' + $(this).val() + '" role ="presentation"><a tabindex="-1" role="menuitem">'+$(this).text()+'</a></li>');
});
$('#newselect_library').append('</ul></div>');
$('#select_library').remove();
$('#newselect_library').attr('id','select_library');
});
$( document.body ).on( 'click', '.dropdown-menu li', function( event ) {
var $target = $( event.currentTarget );
$target.closest( '.btn-group' )
.find( '[data-bind="label"]' ).text( $target.text() )
.end()
.children( '.dropdown-toggle' ).dropdown( 'toggle' );
return false;
});
After executing the code the dropdowns looks like this:
First Dropdown:
First Dropdown Code:
<div class="btn-group btn-input clearfix">
<button type="button" class="btn btn-default dropdown-toggle form-control" data-toggle="dropdown">
<span data-bind="label">Library catalog</span>
<span class="caret"></span>
</button>
<ul id="masthead_search" name="masthead_search" class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
<li value="" role="presentation"><a tabindex="-1" role="menuitem">Library catalog</a></li>
<li value="ti" role="presentation"><a tabindex="-1" role="menuitem">Title</a></li>
<li value="au" role="presentation"><a tabindex="-1" role="menuitem">Author</a></li>
<li value="su" role="presentation"><a tabindex="-1" role="menuitem">Subject</a></li>
<li value="nb" role="presentation"><a tabindex="-1" role="menuitem">ISBN</a></li>
<li value="se" role="presentation"><a tabindex="-1" role="menuitem">Series</a></li>
<li value="callnum" role="presentation"><a tabindex="-1" role="menuitem">Call number</a></li>
</ul>
</div>
Second Dropdown:
Second Dropdown Code:
<div class="btn-group btn-input clearfix open">
<button type="button" class="btn btn-default dropdown-toggle form-control" data-toggle="dropdown">
<span data-bind="label">All libraries</span>
<span class="caret"></span>
</button>
<ul id="select_library" name="branch_group_limit" class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu2">
<li value="" role="presentation"><a tabindex="-1" role="menuitem">All libraries</a></li>
<li value="branch:01" role="presentation"><a tabindex="-1" role="menuitem">Main Library</a></li>
</ul>
</div>
But When I resize my Browser Window and click the first button, my first dropdown doesnt show. And when I click my Second Drop-down the dropdown that is shown is the dropdown which is supposedly in my First Dropdown.
First Dropdown:
Second Dropdown:
Where did I go wrong? please help me.
Ok, so this is not really an answer to your question, but I can't fit it into a comment and I think it needs to be said.
You really need to think carefully as to whether for the sake of changing the colour of drop down items you want to do all this. This is a pretty hefty abuse of HTML and looks like it already has (and will continue to) lead you down a rabbit hole of awkwardness and problems. What you are doing here is throwing away a <select> element and replacing it with a <ul>. You might think 'so what', it looks and works fine, and gives you a greater amount of control over styling, let me try and convince you the why you should just stick with a <select>.
You're going to have a lot of fun posting that data
It looks like you are putting these drop downs in as part of a search form. This means it will be within a <form> element with an action to something like http://myawesomewebsite.com/search, and your search button is a submit button. When you hit submit the page will collect up all the form fields in your form and POST them to the search page. How are you planning on making the page POST your selected option? From the looks of one of the comments, it's possible you have some sort of JavaScript somewhere that is updating a hidden <select>? This will work, but isn't super pretty.
Mobiles/Touchscreens play nicely with selects
You're obviously developing with mobiles/touch screen devices in mind. Great! Too many people still don't bother. Native Select elements will (depending on the specific device/browser) render a nice, scrollable list when you click on it, which is much easier for a user on a small screen to tap than a drop down list that could potentially extend beyond the bottom of the users small screen. People browsing on mobiles will expect to see their devices native list picker when they click a drop down list, don't break peoples expectations without good reason.
Styling/positioning issues
Just like the one you are getting now. A native select list will position itself perfectly well. No hacks or even JavaScript required.
Other things
Remember I mentioned 'abuse of HTML' earlier, if you are expecting the user to select from a list, the element that should be rendered is a <select>, that's how the HTML standard was defined. This also helps accessibility devices understand what your element is and what it should do with it. A drop down list rendered as a <ul> doesn't really make sense to a screen reader, but a <select> with <option> tags make perfect sense. I think Bootstrap does quite a good job of helping screen readers understand elements like it's specialist dropdowns, but I'm no expert on accessibility, so I'm not sure.
I don't know about you, but whenever I use a select box, I click it, hit a key on my keyboard that corresponds to the first letter of what I am after and hit enter. You can't do that with a Bootstrap dropdown (although they are keyboard accessible, so that's good). In your case this perhaps doesn't matter too much, as your options are limited, but with more options this becomes a real time saver for the power users among your user base.
Have you also noticed the fact that in the Bootstrap docs they don't even show you an example of how to use a drop down in this way? The only example they provide are based on menus with links, not replacement selects. There is probably a good reason for that.
So, in conclusion
I'm well aware that there will always be reasons to customize things beyond their original spec, but if you can help it always try to use the elements for what they were designed for. There are plenty more pitfalls than I have mentioned here, and any future developers who take over this code will much prefer a simple select box than a convoluted, jQuery dependent solution. I'll admit that styling of select boxes has always been an issue, but from what I have seen of this particular case, I would recommend just sticking with selects.
However, if you still want to go with this solution then can I recommend you create a Bootply of your problem, with all relevant HTML and JavaScript, so we can have a better crack at replicating your issue.
I hope this has not come off as condescending, and I am by no means saying what you are doing is wrong, as I know nothing of your exact situation, I just felt an explanation as to the potential benefits of keeping it simple might help you or future visitors to this question.
I am using Bootstrap3 in my MVC5 application.
When I use #HtmlHelper.DropDownFor to show a drop down list, bootstrap styles are not getting applied for the combo box in Internet Explorer.
So as a workaround, I wrote a custom code to show a drop down list.
<div class="col-md-6 dropdown">
<button class="col-md-6 form-control input-sm dropdown-toggle" data-val="true" role="button" id="SystemSize" name="dropdown1" data-toggle="dropdown" value="llll">
<span class="caret pull-right"></span>
</button>
<ul class="dropdown-menu pull-right" role="menu" aria-labelledby="dropdown1">
#foreach (var item in items)
{
<li role="presentation"><a role="menuitem" tabindex="-1" href="#">#item.Text</a></li>
}
</ul>
</div>
I referred the following bootstrap link to come up with this approach.
I wrote Jquery to update the text of the button on click of the anchor links in the drop down.
The problem is when I submit the form, this button's text is not getting posted.
How do I solve this?
Normally we use Selects instead of drop-down inside forms. Check following link;
http://getbootstrap.com/css/#forms-controls
Example:
<select class="form-control">
#foreach(var item in items)
{
<option value="#(item.Value)" >#item.Text</option>
}
</select>
If you want to use the Drop-down, then you can use hidden field as follow to store the value and set the value within the function you wrote to update the text.
<input type="hidden" id="SystemSize" name="SystemSize" value="">
Thanks!
I am using bootstrap version 2.0
I have the following html structure -
Now when I click on the Filter by Team the dropdown shows properly. Now when I click on the link, I should be taken to the page. But the links do not work. I mean, when I click on the dropdown elements, they should take me to a url, which they are href'ed to, this does not happen.
<li style="margin-left: 12px;">
<div class="dropdown" style="margin-top: 5px;">
<a class="dropdown-toggle" style="margin-left: -2px;" data-toggle="dropdown" href="#">
Filter by Team
</a>
<ul class="dropdown-menu" data-toggle="dropdown" role="menu" aria-labelledby="dropdownMenu">
<li>
<a tabindex="-1" class="disabled" href="/task/list/orgteam/8/">funvilla</a>
</li>
<li class="divider"></li>
<li>
<a tabindex="-1" class="disabled" href="/task/list/orgteam/6/">Dev Team</a>
</li>
<li class="divider"></li>
<li>
<a tabindex="-1" class="disabled" href="/task/list/orgteam/5/">Design Team</a>
</li>
<li class="divider"></li>
</ul>
</div>
</li>
The fiddle can be found here - http://jsfiddle.net/ktgrw/
The problem is that you have the data-toggle attribute set for the <ul>, that attribute is just for the link that you want to open/close the dropdown, remove it and the links should work.
Here's the updated fiddle with the correct bootstrap version and the attribute removed.
Also the disabled class is used on the toggle link to prevent the opening of the dropdown, you should remove it from your links since it's not serving any real purpose.
The links are working perfectly fine. When we use relative links like href="/task/list/orgteam/5/" we need to be sure that we are running this page on a website where these links exist.
To add to clarity, I added a link to google as the last list item in this new fiddle (click here to view) and because it uses absolute url href="http://www.google.com", it works just fine.
I faced same problem with bootstrap framework and at the end got the solution (worked for me).
Import all your required javascripts, mainly jquery.js .
The problem is that you have the data-toggle attribute set for the <ul>, that attribute is just for the link that you want to open/close the dropdown, remove it and the links should work.
I had the same problem too but after removing the data-toggle attribute, my <a> links are working just fine.
I used the Jquery ui autocomplete combobox http://jqueryui.com/demos/autocomplete/#combobox
I have used the same script given on the page. The combobox is working perfectly. Except when the page size grows and scroll bars appear, and we try to move the scroll bars the toggle button is moving with scroll. So, i tried changing position attribute to relative and also absolute in ui-combobox-toggle class. Still it is not working
This is how the html is rendered (from firebug) before hover on toggle button. After hover ui-state-hover class is added and the toggle button is falling into its place. It is working fine in firefox but in internet explorer the togglebutton is initially moving with scroll and then falling into its place when hovered
<span class="ui-combobox">
<input id="ctl00_ContentPlaceHolder1_ctl00_ddlQuestionShortDescription_2_txtInput" class="ui-state-default ui-combobox-input ui-autocomplete-input ui-widget ui-widget-content ui-corner-left" autocomplete="off" role="textbox" aria-autocomplete="list" aria-haspopup="true">
<a class="ui-button ui-widget ui-state-default ui-button-icon-only ui-corner-right ui-combobox-toggle" style="position: relative;" tabindex="-1" title="Show All Items" role="button" aria-disabled="false">
<span class="ui-button-icon-primary ui-icon ui-icon-triangle-1-s"></span>
<span class="ui-button-text"></span>
</a>
</span>