•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 423,539 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,266 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 2415 | Replies: 2 | Solved
![]() |
•
•
Join Date: Mar 2006
Posts: 12
Reputation:
Rep Power: 3
Solved Threads: 0
Hi All,
I want to display the string captured by using onkeypress event now my problem is i am able to display the string in the text box but i want to display it using <span> or <div> as a string i am attaching the code also :
<html>
<head>
<title>Key Events Example</title>
<script type="text/javascript">
function handleEvent()
{
var FirstTextbox = event.srcElement;
var SecondTextbox = document.getElementById("txt2");
SecondTextbox.value = FirstTextbox.value;
}
var SecondTextbox = event.srcElement;
</script>
</head>
<body>
<p>Type some characters into the first textbox.</p>
<p>
<input type="textfield" id="txt1"
onkeydown="handleEvent()"
onkeyup="handleEvent()"
onkeypress="handleEvent()" ></input></p>
<p><textarea id="txt2" rows="15" cols="50"></textarea></p>
</body>
</html>
I want to display the string captured by using onkeypress event now my problem is i am able to display the string in the text box but i want to display it using <span> or <div> as a string i am attaching the code also :
<html>
<head>
<title>Key Events Example</title>
<script type="text/javascript">
function handleEvent()
{
var FirstTextbox = event.srcElement;
var SecondTextbox = document.getElementById("txt2");
SecondTextbox.value = FirstTextbox.value;
}
var SecondTextbox = event.srcElement;
</script>
</head>
<body>
<p>Type some characters into the first textbox.</p>
<p>
<input type="textfield" id="txt1"
onkeydown="handleEvent()"
onkeyup="handleEvent()"
onkeypress="handleEvent()" ></input></p>
<p><textarea id="txt2" rows="15" cols="50"></textarea></p>
</body>
</html>
•
•
Join Date: Dec 2007
Posts: 75
Reputation:
Rep Power: 1
Solved Threads: 10
Building on what you already have, you would just need to add the desired <span> or <div>, assign it an id, an update that <span> or <div> based on its id value. I have used txt3 on the code below on a <p> tag instead:
<html>
<head>
<title>Key Events Example</title>
<script type="text/javascript">
function handleEvent(e)
{
e = e || window.event;
var FirstTextbox = e.srcElement || e.which;
var SecondTextbox = document.getElementById("txt2");
SecondTextbox.value = FirstTextbox.value;
//this makes a copy of input on txt3, regardless of tag name
document.getElementById("txt3").innerHTML =FirstTextbox.value;
}
</script>
</head>
<body>
<p>Type some characters into the first textbox.</p>
<p>
<input type="textfield" id="txt1"
onkeydown="handleEvent()"
onkeyup="handleEvent()"
onkeypress="handleEvent()" ></input></p>
<p><textarea id="txt2" rows="15" cols="50"></textarea></p>
<p id="txt3"></p>
</body>
</html> > Building on what you already have
>
>
>
onkeydown="handleEvent()
Too many problems with the snippet you posted. First of all, Firefox expects you to pass the event object when invoking the event listener. It should be onkeydown="handleEvent(event)
>
var FirstTextbox = e.srcElement || e.which;
This doesn't make any sense. e.which gives you the code of the key pressed while e.srcElement gives the element on which the event was fired. It should be var FirstTextbox = e.srcElement || e.target; so that the snippet works in both Firefox and IE. Last edited by ~s.o.s~ : Dec 18th, 2007 at 11:39 am.
I don't accept change. I don't deserve to live.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
![]() |
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
ajax asp com cross-browser javascript menu with few lines of code developer development firefox home html internet java javascript javascript smooth scrolling scroll smoothly window document position javascript tab menu with rounded corners generator microsoft microsoft sdk for java microsystems msdn office open platform prevent javascript menu from getting hidden under flash movies programming site software source sql sun vista web
- Previous Thread: Unicode Handling
- Next Thread: Frontend using HTML, backend using Oracle / MS Access



Linear Mode