User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Mar 2006
Posts: 12
Reputation: mithesh is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
mithesh mithesh is offline Offline
Newbie Poster

Help Help needed for onkeypress using java script

  #1  
Dec 17th, 2007
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>
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2007
Posts: 75
Reputation: hielo is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 10
hielo hielo is offline Offline
Junior Poster in Training

Re: Help needed for onkeypress using java script

  #2  
Dec 18th, 2007
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> 
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,851
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 344
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: Help needed for onkeypress using java script

  #3  
Dec 18th, 2007
> 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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 5:36 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC