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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:46:10+00:00 2026-05-24T15:46:10+00:00

I am trying to apply an event to a element dynamically to my application.

  • 0

I am trying to apply an event to a element dynamically to my application.

When I used onclick I get an alert to pop:

el.setAttribute('onclick', 'alert("hi")',0);

When I try to use the onkeypress nothing happens:

el.setAttribute('onkeypress', 'alert("Hi");',0); 

I really need to have the onkeypress available. Is there something that I’m doing incorrectly? Is setAttribute not the best way to achieve what I’m doing? If so what are my alternatives to dynamically setting an event on a specific element?

UPDATE:

To answer a few of the questions below when I do:

el.setAttribute('onkeypress','numericOverride(this)',0);

When I look at the source in Firebug I see the following:

<div id="objSCR" class="txtbox" onmousedown="pfocus(this, '', '');" style="color:#0000FF; width:62px; height:12px; top:76px; left:120px; text-align:right; color:#0000FF;" ptmulti="false" pttype="E" ptlayndx="4" ptdisp="false" ppopup="" plength="12" onchange="numericOverride(this)">4000.00</div>

However, when I change the value of the text I get now output from this function:

function numericOverride(val){
        console.log('hello');
}

When I attempt to do the following:

el.onchange = function() { numericOverride(this) } 

Nothing gets added to the div and I get no result from the function.

  • 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-05-24T15:46:11+00:00Added an answer on May 24, 2026 at 3:46 pm

    Try this (its cross browser):

    function keyPressHandler() { alert("Hi"); };
    
    if (el.addEventListener) {
       el.addEventListener("keypress", keyPressHandler, true);
    }
    else if (el.attachEvent) {
       el.attachEvent("onkeypress", keyPressHandler);
    }
    else {
       el.onkeypress = keyPressHandler;
    }
    

    Demo (note jsfiddle doesn’t play well in IE 7): http://jsfiddle.net/mrchief/BQxWp/2/

    Tested in Chrome, FF5, IE7 (after retrying couple of times) and IE9.

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

Sidebar

Related Questions

I'm trying to use some Apple Events in my Cocoa project to get access
I'm trying to apply border-radius effect on a particular div. The code I used
i'm trying to apply the MVVM pattern in my application with MVVM Light. I've
I am trying to apply global application styles to certain control types, however adding
I am trying to apply smoothing filter to image . But I get this
I'm trying to get the text contents of a .div('.child#') child of my event.target('h6.class'),
How do I use the JavaScript DOM to apply onclick events to links inside
I am trying to apply a background-image to a li element. <ul> <li class=maintablink>&nbsp;</li>
I am trying to apply a style when the mouseenter event is triggered, but
I am trying to use jquery ajax to get data from a php file.

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.