/    Sign up×
Community /Pin to ProfileBookmark

onkeypress is not applied

Hello,

I am creating a input box in a javascript function.
The code I use is:

[CODE]var newInputElem = document.createElement(“INPUT”)
newInputElem.id = ‘TB_Time’
newInputElem.type = ‘text’
newInputElem.style.width = ’50px’
newInputElem.style.border = ‘1px solid’
newInputElem.onkeypress = ‘javascript:SaveEdit(this,’ + Mnum + ‘,”‘ + _ID + ‘”)’
cell.appendChild(newInputElem)[/CODE]

Everything is applied but the onkeypress. This is not aplied to the new edit and will therefor not be called.

Sorry for the beginner question, but I am new to javascript..

Thanks in advance
Shorty

to post a comment
JavaScript

15 Comments(s)

Copy linkTweet thisAlerts:
@KorDec 08.2008 — use an anonymous:
<i>
</i>newInputElem.onkeypress=function(){SaveEdit(this,Mnum,_ID)}
Copy linkTweet thisAlerts:
@BigShorty01authorDec 08.2008 — Thanks!

but this works only in IE.. How about Firefox?
Copy linkTweet thisAlerts:
@KorDec 08.2008 — Should work in Moz as well... Anyway, try [B]onkeyup[/B] or [B]onkeydown[/B] instead of [B]onkeypress[/B].
Copy linkTweet thisAlerts:
@BigShorty01authorDec 08.2008 — all that is in the HTML in Firefox is:

<input id="TB_Time" type="text" style="border: 1px solid ; width: 50px;"/>

In IE it works...

Also using the onkeyup is not working...
Copy linkTweet thisAlerts:
@KorDec 08.2008 — We need to see the whole picture; do you have a test page, or something like that to be seen?
Copy linkTweet thisAlerts:
@BigShorty01authorDec 08.2008 — unfortunatally i can not provide this because it contains sensitive data...

I can however upload the JS file and the aspx file
Copy linkTweet thisAlerts:
@KorDec 08.2008 — Unfortunately, it is of no help to give us the server-side coded pages. We need to see [I]the HTML created[/I] ones (same as you would see in View Page Source / View Source of your browser)
Copy linkTweet thisAlerts:
@BigShorty01authorDec 08.2008 — here it is (hopefully what you need!)
Copy linkTweet thisAlerts:
@KorDec 08.2008 — Oh, I see... you have to capture the [B]event[/B] within the function SaveEdit. Try this
<i>
</i>newInputElem.onkeypress=function(){SaveEdit([COLOR="Navy"]event[/COLOR],Mnum,_ID)}

and change the SaveEdit (insert the blue) as:
<i>
</i>function SaveEdit(e,Mnum,_ID) {
[COLOR="Navy"]if (!e) var e = window.event; [/COLOR] <br/>
switch ((e.which) ? e.which : window.event.keyCode) {
...
Copy linkTweet thisAlerts:
@BigShorty01authorDec 08.2008 — the problem is, that the onkeypress of the input (calling the SaveEdit) is not called. So I do not see how this can help...


unfortunatally your solution did not work.
Copy linkTweet thisAlerts:
@KorDec 08.2008 — Try this:
<i>
</i>...
newInputElem.style.border='1px solid';
[B][COLOR="Blue"]newInputElem['Mnum']=Mnum;
newInputElem['_ID']=_ID;
newInputElem.onkeypress=SaveEdit;[/COLOR][/B]
cell.appendChild(newInputElem)
...

and
<i>
</i>function SaveEdit([B][COLOR="Blue"]e[/COLOR][/B]){
switch ((e.which) ? e.which : window.event.keyCode) {
case 13:
xmlHTTP("GruppenlisteUpdate.aspx", "Origin=Disziplinen&amp;Mnum=" + [B][COLOR="Blue"]this['Mnum'][/COLOR][/B] + "&amp;ID=" + [B][COLOR="Blue"]this['_ID'][/COLOR][/B] + "&amp;Value=" + [B][COLOR="Blue"]this.value[/COLOR][/B], null);
tmptext = [B][COLOR="Blue"]this.value[/COLOR][/B];
DeleteEdit();
return false;
break
case 27:
DeleteEdit(); <br/>
return false;
break;
default:
return true;
} <br/>
}
Copy linkTweet thisAlerts:
@KorDec 08.2008 — ..but... I would advice you to use [B]onkeyup[/B] instead of [B]onkeypress[/B], otherwise Mozilla will return the previous [B]value[/B] of the input (the value [I]before[/I] the onkeypress event action)
<i>
</i>newInputElem.[B][COLOR="Blue"]onkeyup[/COLOR][/B]=SaveEdit;

Copy linkTweet thisAlerts:
@BigShorty01authorDec 08.2008 — Deleted
Copy linkTweet thisAlerts:
@BigShorty01authorDec 08.2008 — The last try worked!

Thank you very very much!

?
Copy linkTweet thisAlerts:
@KorDec 08.2008 — <i>
</i>function SaveEdit(e){
[B][COLOR="Blue"]if (!e) var e = window.event;[/COLOR][/B]
switch ((e.which) ? e.which : window.event.keyCode) {
...
}
×

Success!

Help @BigShorty01 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...