H.fate
H.fate

Reputation: 644

Reset dropdown menu details after re-open or close it

CODE :

<head>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script
    src="https://code.jquery.com/ui/1.9.2/jquery-ui.min.js"
    integrity="sha256-eEa1kEtgK9ZL6h60VXwDsJ2rxYCwfxi40VZ9E0XwoEA="
    crossorigin="anonymous"></script>
</head>
<body>

<div class="dropdown bigdiv">
  <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Dropdown Example
    <span class="caret"></span></button>
  <ul class="dropdown-menu bigul">
    <li>
        <a href="#" id="open_input">open_input</a>
    </li>

    <ul class="list-group test">
    </ul>
  </ul>
</div>

</body>
<script>
    $(document).ready(function () {
        var availableTags = [
            "ActionScript",
            "AppleScript",
            "Asp",
            "BASIC",
            "C",
            "C++",
            "Clojure",
            "COBOL",
            "ColdFusion",
            "Erlang",
            "Fortran",
        ];

        var ul_text = '    <li class="ui-widget">\n' +
            '<form role="form" style="">\n' +
            '                <div class="input-group input-group-sm" style="">\n' +
            '                  <input type="text" class="form-control" id="tags" placeholder="Enter email">\n' +
            '                </div>\n' +
            '              </form>\n' +
            '    </li>'

        $('#open_input').click(function (e) {
            $('.bigul').append(ul_text)

            $('#tags').autocomplete({
                    search: function(event, ui) {
                        $('.test').empty();
                    },
                    source: availableTags,
                    messages: {
                        noResults: '',
                        results: function() {}
                    }
                }).data('autocomplete')._renderItem = function(ul, item) {
                    return $('<li class="list-group-item"></li>')
                        .data('item.autocomplete', item)
                        .append(item.value)
                        .appendTo($('.test'));
                    };
            e.stopPropagation();
        });
    });
</script>

And jsfiddle : http://jsfiddle.net/uMqyn/919/

On my code, dropdown menu can be changed by click open_input or search things with input bar.

But I want that if i close dropdown menu whether by re-click dropdown button or by other empty space clicking or something, dropdown menu will be refreshing as initial status.

In other words, whenever I open dropdown menu, I want that what I can see on dropdown menu is staying the same-just only -

  <ul class="dropdown-menu bigul">
    <li>
        <a href="#" id="open_input">open_input</a>
    </li>

    <ul class="list-group test">
    </ul>
  </ul>

Question : I ask that any changed things on dropdown menu cannot be seen when next dropdown re-open. I want to make dropdown menu's initial status remain unchanged.

How can I do this?

Upvotes: 1

Views: 1004

Answers (1)

L. J.
L. J.

Reputation: 136

Edits: I realized we cannot use .focusout because clicking on the dropdown menu's children elements (e.g. the input texts) will clear out the dropdown menu of its li.ui-widget children.

The solution is to use the Bootstrap dropdown close event hide.bs.dropdown to detect whether we've closed the menu.

$('.bigdiv').on('hide.bs.dropdown', function () {
    $('.bigul .ui-widget').remove()
    console.log('removing')
})

Fiddle: http://jsfiddle.net/uMqyn/924/

Got the idea from: https://stackoverflow.com/a/31444063/3891117

If you're using Bootstrap v2.3.2, in the same link says to use click.dropdown.data-api event.

Upvotes: 2

Related Questions