Pavel Straka Pavel Straka - 3 months ago 16
jQuery Question

JQuery UI autocomplete - one click show, second click hide

I have this autocomplete:

$('#plemena_select').click(function() {
$('#col4_filter').autocomplete("search", "");
$('#col4_filter').focus();
});


I would like to have a link. If the autocomplete is not visible, the click on the link would show it. And if the autocomplete is visible, the click would how it. (One click show, second click hide).
How could I do that?
Thanks everyone.

Answer

i'm not familiar with JQ autocomplete, if you want toggle functions between clicks you can use this:

function hideDiv () {
    //do somthing
    $('.btn').one('click', showDiv);
}

function showDiv () {
    //do somthing
    $('.btn').one('click', hideDiv);
}


$('.btn').one('click', hideDiv);

https://jsfiddle.net/pfnwuk4w/

Or you can use even odd method:

var i = 0;

$('.btn').on('click', function () {

    i += 1;
    var tog = function (someNum) {
        return ((someNum % 2) === 1) ? true : false;
    }
    return ( tog(i) ) ? console.log('on') : console.log('off');

});

https://jsfiddle.net/5oq7wor7/3/

Comments