Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Community Forums
 Community Discussions (All other subjects)
 How to cancel event on javascript?
 New Topic  Topic Locked
 Printer Friendly
Author Previous Topic Topic Next Topic  

GoodMorningSky
Junior Member

122 Posts

Posted - 21 November 2005 :  15:41:47  Show Profile
Hi all.
This is about javascript
I have textbox1. When user keydown "Enter", it shows other Div tag with TextArea1 inside, sets TextArea1.innerText with textbox1.text and puts focus on the TextArea1.
Then TextArea1.innerText is textbox1.text + "NewLine".
I don't want to "NewLine". If I can cancel the enter key event, I can remove the "NewLine" caused by Enter key!

How can I do it?


<div onkeydown="if(event.keyCode==23){hideTxtPane();}" id="divTxtPane" style="border:solid 2px black; background-color:white; display:none;position:absolute; left:97px; top:100px; z-index:1">
  <table>
  <tr><td colspan=2><textarea onkeydown="if(event.keyCode==23){hideTxtPane();}"   id="commentEditor" style="height:50px; width:200px" name="textfield"></textarea></td></tr>
  <tr>
	<td><input style="width:90px;border:solid 1px black;background-color:white;cursor:pointer;"  name="btOk" value="OK" type="button" onClick="applyText();" ></td>
	<td><input style="width:90px;border:solid 1px black;background-color:white;cursor:pointer;"  name="btCancel" value="Cancel" type="button" onClick="hideTxtPane();" onblur="hideTxtPane()"></td>
  </tr>
  </table>  
</div>

<input type="text"  onKeyDown="showTxtPane(this.parentNode.childNodes(0));" value="This is sample text..">

// Here is the code.
var targetNode;
function showTxtPane(_targetNode){
	//enter or mouse clicked.
	if(event.keyCode != 13 && event.keyCode !=0) return;
	//event.cancelBubble = true;
	targetNode = _targetNode;
	targetNode.style.backgroundColor="yellow";
	var div = document.getElementById("divTxtPane");
	var txtEditor = document.getElementById("commentEditor");

	var screenY = document.body.offsetHeight
	var screenX = document.body.offsetWidth;
	div.style.left =  (screenX - div.style.width) / 2;
	div.style.top = (screenY - div.style.height) / 2;
	

	div.style.display = "";
// if I don't put focus, it woks ok. But I have to put the focus!!
	txtEditor.focus();
	txtEditor.innerText = targetNode.value;
	txtEditor.select();
}

Software Engineer.
MCSD.NET, SCWCD

Podge
Support Moderator

Ireland
3776 Posts

Posted - 21 November 2005 :  16:16:49  Show Profile  Send Podge an ICQ Message  Send Podge a Yahoo! Message
Not exactly sure how to do it. Probably something like this;

1. Set a variable (myInnerText) to reference targetNode.value
2. Replace the trailing \n (or \r or \r\n) newline in myInnerText
3. Set txtEditor.innerText = myInnerText;

You can replace using something like this;

myInnerText.replace(/\n/g, "")

The above code would replace all \n newlines, you'll need a regex which will replace a trailing \n or \r or \r\n.

Podge.

The Hunger Site - Click to donate free food | My Blog | Snitz 3.4.05 AutoInstall (Beta!)

My Mods: CAPTCHA Mod | GateKeeper Mod
Tutorial: Enable subscriptions on your board

Warning: The post above or below may contain nuts.
Go to Top of Page

GoodMorningSky
Junior Member

122 Posts

Posted - 21 November 2005 :  16:45:38  Show Profile
Hi, Thank you for reply.
I think my explaination is not enough.

When the textbox1.value and Textarea1.innerText doesn't have any newline charaters. While executing showTxtPane(_targetNode), it doesn' have "new line"! after showTxtPane(_targetNode) is being executed, the character "Enter" keycode is applied to not the textbox1 BUT THE Textarea1 that contains focus.

It means, if I put focus on the new control while executing event handler, the keyevent's keycode(typed word) is applied to new control!!

I just want to stop it applies the keycode to new control inside of the event handler.

If it's not possible is there anyway around to solve this problem?

Software Engineer.
MCSD.NET, SCWCD
Go to Top of Page

Shaggy
Support Moderator

Ireland
6780 Posts

Posted - 23 November 2005 :  08:18:12  Show Profile
What's happening is that the focus is being moved to the textarea onkeydown. As a computer will continue printing a character to screen as long as you are holding down the relevant key on your keyboard, that's what is causing your problem. You could try using onkeyup instead so your script doesn't execute until the enter key is released.


Search is your friend
“I was having a mildly paranoid day, mostly due to the
fact that the mad priest lady from over the river had
taken to nailing weasels to my front door again.”
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Topic Locked
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.29 seconds. Powered By: Snitz Forums 2000 Version 3.4.07