By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,918 Members | 1,852 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,918 IT Pros & Developers. It's quick & easy.

javascript problem in mozilla obj.click()

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.