Connecting Tech Pros Worldwide Forums | Help | Site Map

javascript problem in mozilla obj.click()

rahulgupta
Guest
 
Posts: n/a
#1: Sep 10 '08
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.

Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#2: Sep 10 '08

re: javascript problem in mozilla obj.click()


rahulgupta wrote:
Quote:
i have a textbox and a save_btn which is a hyperlink.
Make the latter a submit button and your problems will go away.
Quote:
function KeyDownHandler(e , btn_Save)
^
It is not a constructor, so it should not look like one.
Quote:
{
>
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, ...
Quote:
{
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.
Quote:
}
else
{
evt.preventDefault();
evt.returnValue=false;
^^^^^^^^^^^^^^^^^^^^^^
Quote:
evt.cancel = true;
^^^^^^^^^^^^^^^^^^
Why two branches for that at all?
Quote:
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.
Quote:
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)"
Quote:
></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>
Kiran Makam
Guest
 
Posts: n/a
#3: Sep 10 '08

re: javascript problem in mozilla obj.click()


On Sep 10, 7:07*pm, rahulgupta <rahu...@gmail.comwrote:
Quote:
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>
Quote:
txtbox1.Attributes.Add("onKeyDown", "javascript:KeyDownHandler(event ,
For event handling you can use addEventListener / attachEvent

Regards
Kiran Makam


dhtml
Guest
 
Posts: n/a
#4: Sep 10 '08

re: javascript problem in mozilla obj.click()


rahulgupta wrote:
Quote:
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
Quote:
>
>
Please somebody help me in solving this problem.
Closed Thread