Solved

Dont hide if visable

Posted on 2013-12-31
13
278 Views
Last Modified: 2013-12-31
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
Comment
Question by:tonelm54
  • 7
  • 6
13 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 39748245
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
 

Author Comment

by:tonelm54
ID: 39748264
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
 
LVL 82

Expert Comment

by:leakim971
ID: 39748271
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:tonelm54
ID: 39748282
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
 
LVL 82

Expert Comment

by:leakim971
ID: 39748297
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
 
LVL 82

Expert Comment

by:leakim971
ID: 39748309
I gave you the wrong link, here my first attemp : http://jsfiddle.net/LtLs3/2/
0
 

Author Comment

by:tonelm54
ID: 39748310
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
 
LVL 82

Expert Comment

by:leakim971
ID: 39748315
Sorry, forgot the CSS : http://jsfiddle.net/LtLs3/3/
0
 

Author Comment

by:tonelm54
ID: 39748317
Sorry, but when I run http://jsfiddle.net/LtLs3/2/ it doesnt show the box at all :-(
0
 

Author Comment

by:tonelm54
ID: 39748327
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
 
LVL 82

Expert Comment

by:leakim971
ID: 39748353
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
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39748483
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
 

Author Closing Comment

by:tonelm54
ID: 39748499
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question