?
Solved

javascript block focus event

Posted on 2012-04-13
6
Medium Priority
?
455 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

800 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