Solved

javascript block focus event

Posted on 2012-04-13
6
431 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

740 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