javascript problem in mozilla obj.click()

R

rahulgupta

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.
 
T

Thomas 'PointedEars' Lahn

rahulgupta said:
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.

}
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
 
K

Kiran Makam

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">
txtbox1.Attributes.Add("onKeyDown", "javascript:KeyDownHandler(event ,

For event handling you can use addEventListener / attachEvent

Regards
Kiran Makam
 
D

dhtml

rahulgupta said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top