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 9010413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:27:06+00:00 2026-06-16T02:27:06+00:00

I have a page with 2 javascript events, one triggered from the mouse, the

  • 0

I have a page with 2 javascript events, one triggered from the mouse, the other from the keyboard. When I load the page I can get one or the other to work, but not both. If I press a key first that function will run, but then I cannot do the mouse click or another keystroke. And vice versa. I know jQuery makes this easier, but I’d rather not have my users download that for each page, so I’m trying to do this the old fashioned way. I have read as much about javascript events as I can find but I’m stuck here with this one …

thanks in advance, Brad.
NOTE: in Chrome and Safari I get the above results, Firefox and Opera will only work with the keystroke function

<html>
<head>
<script>
function create(event) {
var x=event.clientX-14;
var y=event.clientY-33;
var output = document.write("<p id=\"text\" style=\"background-color: white; position:    absolute; top:" + y + "px;left:" + x + "px;\";>You're Text, your M@jesty!</p>");
}
function type(event)
{
var letter_in = event.keyCode;
var letter = String.fromCharCode(letter_in);
//var shift = event.shiftKey;
//if (shift === false) {letter = String.toLowerCase;}
document.write(letter);
}
</script>
</head>

<body onmousedown="create(event)" onkeydown="type(event)">

</body>
</html>
  • 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-16T02:27:08+00:00Added an answer on June 16, 2026 at 2:27 am

    It’s because you’re using document.write() which clears everything else on the page, including your handlers.

    Calling document.write() after the document has been loaded implicitly calls document.open(), which clears the document.

    So to fix this, you want to use something like innerHTML to only update the contents of an element within your page.

    See this example:

    <html>
      <head>
    
      </head>
    
      <body onmousedown="create(event)" onkeydown="type(event)">
        <div id="foo"></div>
    
        <script>
          function create(event) {
            var x=event.clientX-14;
            var y=event.clientY-33;
            var output = document.getElementById("foo").innerHTML = "<p id=\"text\" style=\"background-color: white; position:    absolute; top:" + y + "px;left:" + x + "px;\";>You're Text, your M@jesty!</p>";
          }
              function type(event)
              {
                var letter_in = event.keyCode;
                var letter = String.fromCharCode(letter_in);
                //var shift = event.shiftKey;
                //if (shift === false) {letter = String.toLowerCase;}
                document.getElementById("foo").innerHTML = letter;
              }
        </script>
      </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can you setup a variable for javascript in one page and have another page
Need some help from javascript gurus. I have one page where http://www.google.com/finance/converter is embedded
I have one page website only using HTML, CSS and JavaScript. I want to
I have tooltip(prototip, but it doesnt matter) on my page in javascript and I
I have a web page (JavaScript) that has two types of events: Click event
Okay, so I have this page, which has one Ajax:AsyncFileUploader also some other ajax
I have a javascript page which checks an email and username, this works fine
I have a page with some very basic examples that use HTML/CSS/JavaScript. I'd like
I have a page that is generate almost entirely by javascript. I have a
Assume you have a page in ASP.NET where it makes sense to use JavaScript/jQuery

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.