Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 286
  • Last Modified:

Dont hide if visable

Im trying to construct my own AutoComplete for a website Im making, as the premade Jquery UI Autocomplete doesnt do everything I want to do.

Ive got quite far, but stuck on 2 issues, the first is not hiding the autocomple if another element that belongs to the class has focus.

I thought if I used:-
if ($('.AutoComplete').not(':focus')) {
Then this would not run, however doesnt seem to work as it should.

My code works fine for the first textbox, but once lost focus it moves but shouldnt hide, but it does :-(

Ive saved a quick POC at JSFiddle in the hope an expert will be able to show me the issue and hopefully show me how to fix it :-)


Thank you in advance
0
tonelm54
Asked:
tonelm54
  • 7
  • 6
1 Solution
 
leakim971PluritechnicianCommented:
What about : http://jsfiddle.net/LtLs3/1/

$(document).ready(function () {
    $(".AutoComplete").focus(function () {
        $(this).on("blur", function () {
            $(this).off("blur");
            $("#AutoCompleteWindow").hide();
        });
        $("#AutoCompleteWindow").show();
        $("#AutoCompleteWindow").position({
            "my":"left top",
            "at":"left bottom",
            "of":$(this)
        });
    });
});

Open in new window

0
 
tonelm54Author Commented:
Thanks leakim971 but it still does the same thing, if you select the first textbox, the green box appears (as it should) but then if I select the second textbox the green box moves (which it should) but then disappears.

The reason I put the setTimeout in was so the user can click on the hyperlink and it will run the click method, otherwise it runs the blur event first then cant run the click.
0
 
leakim971PluritechnicianCommented:
your code say : show the box if this class of field get focus

so at which moment if should not show the box? and why ?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
tonelm54Author Commented:
So the idea is to have multiple text boxes on one page, the green box will move blow each one as selected if there isnt any textbox selected (eg the document) then the green box disappears.

The final idea will be to present something different depending on the element selected.
0
 
leakim971PluritechnicianCommented:
so you move to another textbox (which get focus)
you say you want to hide the box

but at which moment do you want to display this box for this new textbox?
0
 
leakim971PluritechnicianCommented:
I gave you the wrong link, here my first attemp : http://jsfiddle.net/LtLs3/2/
0
 
tonelm54Author Commented:
The green box should move instantly to the new textbox if selected, if not then just hide.

If the hyperlink is clicked then it puts the a value in the textbox and then hides.
0
 
leakim971PluritechnicianCommented:
Sorry, forgot the CSS : http://jsfiddle.net/LtLs3/3/
0
 
tonelm54Author Commented:
Sorry, but when I run http://jsfiddle.net/LtLs3/2/ it doesnt show the box at all :-(
0
 
tonelm54Author Commented:
Got the http://jsfiddle.net/LtLs3/3/ which works, but the hyperlink doesnt work now, so when hyperlink is clicked it should add its value to the textbox:-
http://jsfiddle.net/LtLs3/4/
0
 
leakim971PluritechnicianCommented:
Test page : http://jsfiddle.net/LtLs3/7/

$(document).ready(function () {
    $(".AutoComplete").focus(function () {
        $(this).addClass("active");
        $(this).on("blur", function () {
            var $this = $(this);
            setTimeout(function() {
                $this.off("blur");
                $("#AutoCompleteWindow").hide();
                $this.removeClass("active");
            }, 400);
        });
        $("#AutoCompleteWindow").show();
        $("#AutoCompleteWindow").position({
            "my":"left top",
            "at":"left bottom",
            "of":$(this)
        });
    });    
    $("#AutoCompleteWindow").on("click", "a", function(evt) {
        evt.preventDefault();
        $(".AutoComplete.active").val( $(this).text() );
    });
});

Open in new window

0
 
leakim971PluritechnicianCommented:
another one : http://jsfiddle.net/LtLs3/8/

$(document).ready(function () {
    $(".AutoComplete").focus(function () {
        $(".AutoComplete.active").removeClass("active");
        $(this).addClass("active");
        $("#AutoCompleteWindow").show();
        $("#AutoCompleteWindow").position({ "my":"left top", "at":"left bottom", "of":$(this) });
    }).blur(function() {
        if(!$("#AutoCompleteWindow").hasClass("active")) {
            $("#AutoCompleteWindow").hide();
        }
    });

    $("#AutoCompleteWindow").on("click", "a", function(evt) {
        evt.preventDefault();
        $(".AutoComplete.active").val($(this).text());
        $("#AutoCompleteWindow").hide();
    }).hover(function() {
        $(this).addClass("active");
    }, function() {
        $(this).removeClass("active");
    }).on("blur", "a", function() {
        $("#AutoCompleteWindow").hide();
    });
});

Open in new window

0
 
tonelm54Author Commented:
This really works well, and it can it can allow other controls inside the box to allow for other criteria (something that I was going to try with version 2).

Example here:-
http://jsfiddle.net/3JkQM/

Thank you for your patience to sort this :-)
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now