Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

javascript block focus event

Posted on 2012-04-13
6
Medium Priority
?
473 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:Gurvinder Pal Singh
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
Technology Partners: 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:
Gurvinder Pal Singh earned 1000 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 1000 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

This article discusses how to create an extensible mechanism for linked drop downs.
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 to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

609 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