Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7753737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:07:39+00:00 2026-06-01T12:07:39+00:00

The onFocus event keeps firing on page load and doesn’t seem to work when

  • 0

The onFocus event keeps firing on page load and doesn’t seem to work when the element goes into focus. I only want the alert to fire off when the the input comes into focus not on page load

//Input
var input = document.getElementById('phonenumber');
//onfocus execute function
input.onFocus = alert('test')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:07:41+00:00Added an answer on June 1, 2026 at 12:07 pm

    You’re calling the function alert and assigning its return value (undefined) to the focus handler. Try this instead:

    input.onfocus = function() { alert('test'); };
    

    Or, perhaps more understandably:

    function inputFocused() {
        alert('test');
    }
    input.onfocus = inputFocused;
    

    Note that there are no parentheses after inputFocused in the assignment. We want to set onfocus to the function itself, not to the result of calling the function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The window.onfocus event does not stop firing in Chrome. It is fine in every
I like to know what the onFocus event is (if there will be any)
ok, so I have this small block of text: function onfocus(event) { if ($(this).val()
What is the difference between the onFocus and onMouseEnter event?
My page should make notification sounds only when the window is in the background.
Let, <span onclick=foo() onfocus=goo()>Have Event</span> Here I give this just for example. Here with
I have a following code which I tried to assign onfocus event to the
I'm trying to set a focus event on all my CKEditor textareas on the
Here it is: //Disable KeyboardNavigation document.getElementById(author).onfocus = function() { document.onkeyup = null; }; document.getElementById(email).onfocus
Since I need to do some checks depending on which control is on focus

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.