0

I have a simple dropdown that opens up a search field when you click it. Even though I have the text field of this search set to autofocus, it's not working for all browsers.

What method of Javascript/jQuery would I use to check if the containing UL css display is set to block, so that I can force the focus to be on the field using .focus().

HTML:

<a href="#" id="quickSearch" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Quick Search</a>
<ul class="dropdown-menu" role="menu">
  <li id="li-quicksearch">
      <form id="mainSearch" class="form-search">
        <p>
            <input type="text" id="inputSearch" class="form-control" placeholder="Quick Search" required="" autofocus autocomplete="off">
            <button type="submit">SUBMIT</button>
        </p>
      </form>
  </li>
</ul>
2
  • 1
    If it's displayed by media query, use resize event. If it is displayed by JS, do your action when you display it. There is no other way (or I don't see any other way). Commented Feb 2, 2015 at 14:24
  • 1
    Simply add document.getElementById('inputSearch').focus(); when the field is shown Commented Feb 2, 2015 at 14:24

4 Answers 4

2

EDIT: There is no css change event so you'll have to approach the problem in 1 of 2 ways.

  • check the dom element in set intervals to see if its css has changed

  • trigger an event when the css of the dom element is changed by user interaction/your code.

the first way will look something like this:

var element = $(".dropdown-menu");
function checkForChanges()
{
    if (element.css('display') == 'block')
    {
        // do your .focus() stuff here
    }
    setTimeout(checkForChanges, 500); // does this every half second.
}

or the second way:

$('.toggle').on('click', function() {
    $('.dropdown-menu').toggle();
    $('.dropdown-menu').trigger('change');
});
$('.dropdown-menu').on('change', function(){
    if($(this).css(.css('display') == 'block')
    {
        // do your .focus() stuff here
    }
});
Sign up to request clarification or add additional context in comments.

5 Comments

this is not executed on the fly, only when the page is loaded.
ahh, sorry, i misunderstood the issue. what exactly triggers the show/hiding of the ul?
@Murphy1976, please check my edit, to see if that's suitable to your needs :]
"EDIT: There is no css change event so you'll have to approach the problem in 1 of 2 ways." "trigger an event when the css of the dom element is changed by user interaction/your code." doesn't make sense. You contradict yourself within those 2 sentences.
@Winchestro the two sentences dont contradict. theres no type of "onChangeOfCss" event you can track. you can use the event that triggers the change of the css, but there is no "listen for the css change" event
1

You can check the display value of the ul using pure JavaScript with this:

JS:

var display = document.getElementById('dropdown-menu')[0].style.display;
if (display === 'block') {
  //do what you want.
}

Or using jQuery:

if ($('.dropdown-menu').css('display') === 'block') {
   //do what you want.
}

Comments

1

It looks like you are using bootstrap to create the dropdown. If that is the case you can use the "shown" event. However you need to attach the event on a container element.

Html

<div class="quickSearchContainer">
    <a href="#" id="quickSearch" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Quick Search</a>
    <ul class="dropdown-menu" role="menu">
        <li id="li-quicksearch">
            <form id="mainSearch" class="form-search">
                <p>
                    <input type="text" id="inputSearch" class="form-control" placeholder="Quick Search" required="" autofocus autocomplete="off">
                    <button type="submit">SUBMIT</button>
                </p>
            </form>
        </li>
    </ul>
</div>

Javascript

$('#quickSearchContainer').on('show.bs.dropdown', function () {
    $('#inputSearch').focus();
});

Comments

0

I want to thank everyone for their input, but the working solution that I found was to modify the bootstrap JS to allow for an autofocus on toggleClass of the OPEN for the dropdowns. Everyone gets kudos!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.