Solved

Dont hide if visable

Posted on 2013-12-31
13
273 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
 

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Alert not firing 2 32
html select on change 3 25
jquery .hide 1 19
CSS Divs in Safari 3 19
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now