Solved

javascript block focus event

Posted on 2012-04-13
6
412 Views
Last Modified: 2012-04-13
hi

suppose we a input text

<input id=myField type=.. onFocus="call..">

Open in new window


if the user  clicks on the field, the focus gets triggered and the action is called.

OK.

but i want that when i manually focus to the myField with this code

document.getElementById("myField").focus();

Open in new window


, the focus event SHOULD NOT triggered. how can i achieve this?

i do not want to that with using some blocker flags or global variables to keep track of whether focus should be fired or not.
0
Comment
Question by:benchpresser
  • 3
  • 2
6 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37841507
before calling this focus() method, unbind the focus event handler
document.getElementById("myField").onfocus = null;
document.getElementById("myField").removeEventListener("onfocus");

check this
http://www.quirksmode.org/js/events_advanced.html

and then set it back again after calling focus method
0
 

Author Comment

by:benchpresser
ID: 37841526
On this page I explain the two advanced event registration models: W3C’s and Microsoft’s. Since neither is cross–browser supported, their use is, for the moment, deprecated.

does it work on last ie and last ff?
0
 

Author Comment

by:benchpresser
ID: 37841530
my scenario is:

a have page user clicks on a textfield. it has focus registered to it. it invokes some ajax request and goes to server. i want that the focus (blinking of cursor) remains on this textfield after we got the response. because the blinking gets lost after the response, i manually focus to that textfield when i handle the response.

because i manually set focus to this textfield it again fires focus and an endless loop starts.
i want to prevent the second firing of focus action. but of couse if the user clicks another textfield and again clicks back to our initial field, it should accept and trigger focus event again.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 40

Accepted Solution

by:
gurvinder372 earned 250 total points
ID: 37841614
<<does it work on last ie and last ff?>>
Yes,
document.getElementById("myField").onfocus = null;
should work on all browsers
0
 
LVL 20

Assisted Solution

by:Proculopsis
Proculopsis earned 250 total points
ID: 37841800
Take a look at this jsfiddle - jQuery allows a bit more flexibility with the trigger method.

function eventFocus(event, parameter) {
    if (typeof(parameter) == "undefined") {
        if (typeof(window.manualFocus) == "undefined") {
            $(this).val("User Focus"); // <<== User focus code here
        }
        delete window.manualFocus;
    } else if (typeof(parameter) != "undefined") {
        $(this).val("Manual Focus"); // <<== Manual focus code here
        window.manualFocus = true;
    }
}

function focus() {
    $("#myField").trigger("focus", true);
}

Open in new window

0
 

Author Comment

by:benchpresser
ID: 37841957
thank you very much it was very helpful.

how can we unbind or undelegate an action which was defined like that below:

<input ... onFocus="myAction">

Open in new window


Because if we have the form below we can unbind it.

<input>
<script>
$('#myAction").bind("focus", function() )..

</script>

Open in new window


i can unbind that.

but i can unbind when defined as onFocus=".."
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

Title # Comments Views Activity
Google Analytics setup 5 50
How to add a stringified JSON string to an IP address? 11 60
Ouput in html in powershell 2 21
Powershell script does not exit 4 29
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

830 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