473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

javascript problem in mozilla obj.click()

i have a textbox and a save_btn which is a hyperlink. when ever enter
key is pressed there is a javascript which check if the name in the
textbox contains any special characters. but when we press enter it do
page postback and does not check for special characters.

i have written a javascript function which stops the post back on
enter press in textbox. and then i raise the onclick event by
document.getElementById(btn_Save).click(). in IE it works fine, but,
for mozilla and safari it just do the post back on enter key press.
I also tried to dispatch a event for mozilla but it isn't working.
Here is my code

function KeyDownHandler(e , btn_Save)
{
var browser=navigator.appName;
evt = e || window.event ;
if (evt.keyCode == 13|| evt.which == 13)
{
if(browser == "Microsoft Internet Explorer" )
{
evt.returnValue=false;
evt.cancel = true;
document.getElementById(btn_Save).click();
}
else
{
evt.preventDefault();
evt.returnValue=false;
evt.cancel = true;
var e1 = document.createEvent('HTMLEvents');
e1.initEvent('click', false, false);
document.getElementById(btn_Save).dispatchEvent(e1 );
}
}
}

and this is a code behind

txtbox1.Attributes.Add("onKeyDown", "javascript:KeyDownHandler(event ,
'"+ btn_Save + "');");
Please somebody help me in solving this problem.
Sep 10 '08 #1
3 2520
rahulgupta wrote:
i have a textbox and a save_btn which is a hyperlink.
Make the latter a submit button and your problems will go away.
function KeyDownHandler(e , btn_Save)
^
It is not a constructor, so it should not look like one.
{

var browser=navigator.appName;
evt = e || window.event ;
if (evt.keyCode == 13|| evt.which == 13)
{
if(browser == "Microsoft Internet Explorer" )
Browser sniffing is an error-prone practice that is unnecessary here, ...
{
evt.returnValue=false;
evt.cancel = true;
document.getElementById(btn_Save).click();
.... you should feature-test whether your objects have those properties instead.

<http://PointedEars.de/scripts/test/whatamipp.
}
else
{
evt.preventDefault();
evt.returnValue=false;
^^^^^^^^^^^^^^^^^^^^^^
evt.cancel = true;
^^^^^^^^^^^^^^^^^^
Why two branches for that at all?
var e1 = document.createEvent('HTMLEvents');
e1.initEvent('click', false, false);
`click' is implemented as a *mouse* event, so it would have to be
`MouseEvents' and the initEvent() call would be missing several arguments.
document.getElementById(btn_Save).dispatchEvent(e1 );
}
}
}

and this is a code behind

txtbox1.Attributes.Add("onKeyDown", "javascript:KeyDownHandler(event ,
'"+ btn_Save + "');");
Wrong event, it is only suitable for printable characters. And since no
standard object would have an `Add' method, you are using some library; not
showing it makes it impossible for others to be sure what it does. But you
really do not need any of this. Quick hack:

<script type="text/javascript">
function keyDownHandler(e)
{
var c = e.charCode || e.keyCode;
if (c == 13)
{
postback();
return false;
}

return true;
}

<form action="..." method="post">
<textarea
onkeypress="if (typeof event != 'undefined')
return keyDownHandler(event)"
></textarea>
<input type="submit" value="Save">
</form>

However:

If you mean `textarea' by "textbox", there is no reason not to use an
`input' element instead, for you do not seem to want or need multi-line
input/display.

If you mean an `input' element by that, you need no scripting at all.
PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Sep 10 '08 #2
On Sep 10, 7:07*pm, rahulgupta <rahu...@gmail.comwrote:
i have written a javascript function which stops the post back on
enter press in textbox.
If a form contains only a text field, it gets submitted when 'Enter'
key is pressed. You can prevent this without using javascript by
adding another textfield. You can use display:none to hide the dummy
textfield and if you don't give a name it will not get submitted also.

Code:
<form ....>
<input type="text" name="abc" value="actual text field">
<input type="text" style="display:none">
</form>
txtbox1.Attributes.Add("onKeyDown", "javascript:KeyDownHandler(event ,
For event handling you can use addEventListener / attachEvent

Regards
Kiran Makam
Sep 10 '08 #3
rahulgupta wrote:
i have a textbox and a save_btn which is a hyperlink. when ever enter
key is pressed there is a javascript which check if the name in the
textbox contains any special characters. but when we press enter it do
page postback and does not check for special characters.
Use onsubmit for the form.

<form onsubmit="alert('checking data'); return false;"
action="http://google.com/">
<input type="text">
<input type="submit">
</form>
You can use attachEvent/addEventListener pair or a legacy handler.

attachEvent - e.returnValue = false; // MSIE Events
addEventListener - e.preventDefault(); // DOM Event
onsubmit - return false; // Legacy

Don't use browser detection. Use feature detection.

if(form.attachEvent) {
form.attachEvent('onsubmit', checker);
} else if(form.addEventListener) {
form.addEventListener('submit', checker, false);
}

Garrett
>

Please somebody help me in solving this problem.
Sep 10 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: KathyB | last post by:
Hi, I'm creating my first (so please be patient) web app in asp.net. I have a mainframe.aspx and within that a leftframe.aspx and rightframe.aspx. The leftframe holds some variables and has a...
1
by: memememe | last post by:
So I know this is not the right newsgroup but im kinda lazy to add another newsgroup to my outlook. Is there a way for meto know where on the page someone clicked using javascript? and by where I...
1
by: TJS | last post by:
asp:ImageButton only produces desired effect if dbl-clicked how can I apply javascript to convert single click into dbl-click ?
4
by: Ian Kelly | last post by:
Hi All, I have an .net form that is split into two frames. The left frame has a tree that displays a list of all the customers. The right frame displays the appropriate clients information. ...
8
by: ahaupt | last post by:
Hi all, I have to use some javascript to "click" a search button on my aspx page. The server side btnSearch_Click executes prefectly, but there's a Response.Redirect within the method, that...
15
by: Encapsulin | last post by:
Hello everybody. I'm trying to change src of quicktime embedded object with javascript: <html><body> <script language="JavaScript"> function Exchange() { document.qtvr.src = "sample2.pano";...
1
by: anthonyungerman2000 | last post by:
Is it possible to simulate a mouse click at a certain point within a frame using JavaScript? Thanks
1
by: sonaldewle | last post by:
I have a master page which includes a menu control. The menu control is binded through a web.sitemap file. I want to call a javascript on click of each menu item. I have added an attribute in master...
8
by: Deft.Jab | last post by:
This problem only exists in IE, due to UI restrictions I'm using a href in an error label to bring up a modal popup. I'm using a callback to filter a grid on the popup. The problem: The href...
1
by: swatiminiyar | last post by:
I m using a form which is posted through javascript on click of a image.Its working fine in IE but not in mozilla. can anybody help me out? my code is print("<form name=\"topPanelForm\"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.