Solved

Dont hide if visable

Posted on 2013-12-31
13
277 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

810 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