In IE8 enter key in a form does not work - forms

I have a problem that in IE8 the enter does not work to submit a form. I have generated a test page to expose this problem. It seems that displaying the form in the onLoad function disables results that the enter button does not trigger a submit anymore. Is this a bug in IE8 or is it some security issue?
The code to reproduce this is:
onload = function() {
document.getElementById('test').style.display = 'block';
}
#test {
display: none;
}
<form id="test" method="get" action="javascript:alert('woei!')">
<input type="text" name="user" value="">
<input type="password" name="pw" value="">
<input type="submit" value="submit" id="submit">
</form>

I have found a proper solution and wanted it to share with u guys.
Instead of using <input type="submit...>, use <button type="submit"...>.
This will do exactly the same in the other browsers (IE6-7, FF3) AND works in IE8. :)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<style type="text/css">
#test {
display: none;
}
</style>
<script type="text/javascript">
onload = function() {
document.getElementById('test').style.display = 'block';
};
</script>
</head>
<body>
<form id="test" method="get" action="javascript:alert('woei!')">
<input type="text" name="user" value="" />
<input type="password" name="pw" value="" />
<button type="submit" value="submit" id="submit"></button>
</form>
</body>
</html>

$("form input").keypress(function (e) {
if(e.which === 13) {
$("form").submit();
}
});
Above is a proper fix. Ref: IE Not submitting my form on enter press of enter key?

I think everthing is much more complicated than you think...
when a form's display value is set to none with a css class or just with a style attribute on page inital, hitting the enter key in a text field does not work only if you have more than one input field with text type... if you have one text field it works fine.. if you have more than one, it does not fire form submission...
Here i made a demo...
Works Fine (Normal Form)
http://yasinergul.com/stackoverflow/ie8-enter-key-bug/one.html
Works Fine (Form hidden & set back visible but it's with one text input)
http://yasinergul.com/stackoverflow/ie8-enter-key-bug/two.html
Does Not Work (Form hidden & set back visible but it's with two text input)
http://yasinergul.com/stackoverflow/ie8-enter-key-bug/three.html
i think the best approach is to give a .hidden class to the object but not setting display:none for this css selector. you can make it hidden with jquery like
$(".hidden").hide();
as the page loads the form is shown for miliseconds but gets hidden after jquery works...

I can't say if it is a bug exactly, but I can confirm that the behavior you report has changed in IE 8... and I imagine it is probably a bug, not an deliberate change.
If the form is set with CSS display:none the default submit button behavior doesn't work.
Other browsers, including IE 7 (or even IE 8 using IE 7 standard compatibility mode) do not have problems.
I've worked around the problem myself by just using height:0px; in the CSS, then having javascript set the appropriate height when I want to show the form. Using height instead, the default enter key submit behavior seems to work normally.

Old ticket, but I'd like to add what I think is the explanation:
IE8 does the following peculiar thing: the Enter key will submit the form, but any
<input type="submit" name="MySubmitButton" value="I hope I detect THIS VALUE in POST" />
won't be sent in the POST.
IE9 changes the behavior and sends the value. Chrome has always sent the value, as far as my tests have shown.
I hope this helps...

For any future users stumbling upon this question:
What worked for me was adding a DOCTYPE:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

A fix to what #Jitendra Pancholi suggested- now it submits only the form we want, not all of them
$("form input").keypress(function (e) {
if(e.which === 13) {
$(this.form).submit();
}
});

I tried it in IE8 and it works for me. You have to make sure that part of the form has focus though.
Javascript has a focus function that you can use to set the focus if that's what you need.
var textbox = document.getElementById("whatever name input box's id will be");
if(textbox) textbox.focus();

You may want to add a onkeyup event to your input boxes so that if you hit an enter in the input box then it will also submit.
As CodePartizan mentioned, you need the focus on the button otherwise, so if you tab over to the button, or click on it, it seems to work for me also.

I believe Yasin has got the point.
I just had the same problem: multiple text fields within a form whose visibility is "hidden".
My workaround (to prevent the form from flashing) is to set the form opacity to 0 in the css, and then customise its style settings with jQuery on document ready.
I believe this is not something to fix with JS.

Yeah, I was bitten by this bug too today. I found this workaround, though (diff from the OP):
<script type="text/javascript">
onload = function() {
document.getElementById('test').style.display = 'block';
+ document.getElementById('test').innerHTML =
+ document.getElementById('test').innerHTML;
}
</script>
</head>
Simply recreate the contents of the form from the contents of itself. Yikes. But it works. (Famous last words...)

This works for me in IE8. I had this problem when using only one input field.
Read more: http://www.rachaelarnold.com/dev/archive/enter-key-form-submission-bug#ixzz2Y5Zwgj2k

I had the same issue with ie and none of the solutions helped until I read this:
http://www.rachaelarnold.com/dev/archive/enter-key-form-submission-bug#ixzz2Y5Zwgj2k
my form only had one input field....duh! :)

Found a working solution.
Make the submit button invisible instead of using display:none;
input#submit {
color: transparent;
background: transparent;
border: 0px;
}

Related

How to make fancybox close after hitting "submit button"

My form comes from Hubspot and is embedded into a fancybox.
I would like the box to close as soon as the person hits "submit" but instead it redirects and stays open.
Secondly, I tried entering the code to clear the content box after submission but the old data stays in the fields after reopening.
How do I make the box close after they "submit"?
How do I code so that fields clear after each submission?
http://online.saintleo.edu/FancyBox2/FancyBox.html
In advance, thank you. You guys/gals are great.
After inspecting your Fancybox I found this html:
<form accept-charset="UTF-8" enctype="multipart/form-data" id="hsForm_bc762bf8-087a41ec-8f55-263df96f988a" class="hs-custom-form stacked hs-form" action="https://forms.hubspot.com/uploads/form/v2/206683/bc762bf8-087a-41ec-8f55-263df96f988a" method="POST" novalidate="novalidate">
It works as expected when I add target="_top" to it.
Your form.html may have a form tag. Add target="_top" to it.
See this link Link here
Add target="_top" in form tag
<form target="_top">

Mobile Form Fields - Losing Focus

I'm developing a mobile/web application. When I view form fields in a mobile browser, like iPhone's safari or even the chrome mobile app, I can't lose focus on form fields unless I press another form element or "Done" on the keyboard..
I've even completely stripped my code down to:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<form>
<input type="text"/>
</form>
</body>
</html>
and I still can't lose focus (in mobile browsers) of the text field by pressing elsewhere!
Anyone have any ideas?
Try:
document.body.addEventListener("click", function(evt) {
var nodeName = evt.target.nodeName.toLowerCase();
if (nodeName !== "input" && nodeName !== "textarea" && nodeName !== "select") {
document.activeElement.blur();
}
});
I had the same problem and I found this hack works
document.body.firstElementChild.tabIndex = 1
This makes the first element in the body focusable thus when you click "outside" the input it loses focus. At the same time you don't see any visual glitches on that focused element because there is not style defined (unless your first body child element is something other than div)

Avoid modal dismiss on enter keypress

I have set up a bootstrap modal with a form inside it, I just noticed that when I press the Enter key, the modal gets dismissed.
Is there a way not to dismiss it when pressing Enter?
I tried activating the modal with keyboard:false, but that only prevents dismissal with the ESC key.
I just had this problem too.
My problem was that i had a close button in my modal
<button class="close" data-dismiss="modal">×</button>
Pressing enter in the input field caused this button to be fired. I changed it to an anchor instead and it works as expected now (enter submits the form and does not close the modal).
<a class="close" data-dismiss="modal">×</a>
Without seeing your source, I can't confirm that your cause is the same though.
Just add the type="button" attribute to the button element, some browsers interpret the type as submit by default.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#Attributes
This applies for all the buttons you have in the modal.
<button type="button" class="close" data-dismiss="modal">×</button>
I had this problem even after removing ALL buttons from my Bootstrap Modal, so none of the solutions here helped me.
I found that a form with a single text field would cause the browser to do a form submit (and result in dismiss), if you hit Enter while keyboard focus is on the text field. This seems to be more of a browser/form issue than anything with Bootstrap.
My solution was to set the form's onsubmit attribute to onsubmit="return false"
This may be a problem if you are actually using the submit event, but I'm using JS frameworks that generate AJAX requests rather than doing a browser submit, so I prefer disabling submit entirely. (It also means I don't have to manually tweak every form element that might trigger a submit).
More info here: Bootstrap modal dialogs with a single text input field always dismiss on Enter key
I had same problem, and i solved it with
<form onsubmit="return false;">
but there is one more solution, you can add dummy invisible input, so your form would look like this:
<form role="form" method="post" action="submitform.php">
<input type="text" id="name" name="name" >
<input type="text" style="display: none;">
</form>
You can put the login button before the cancel button and this would solve the issue you are having as well.
<div class="modal-footer">
<button type="submit" class="btn primary">Login</button>
<button type="submit" class="btn" data-dismiss="modal">Cancel</button>
</div>
I had a similar experience just now and the way I solved it was instead of using a tag, I changed the tag to an tag with type="button". This seemed to solve the problem of pressing the "enter" key and dismissing the bootstrap modal.
I had this problem too and I solved it this way. I added onsubmit to form. I also wanted to be able to use enter key as a saving key so I added save_stuff() javascript to onsubmit. return false; is used to prevent the form submit.
<form onsubmit="save_stuff(); return false;">
...
</form>
<script>
function save_stuff(){
//Saving stuff
}
</script>

jQuery mobile show and hide don't seem to work on iPhone

I'm trying to use jQuery show and hide which seem to work ok in Safari, when I try it on the iPhone, it doesn't work at all. Here is my code;
<script type="text/javascript">
$("#showSearch").click(function () {
$("#searchform").show(1000);
});
</script>
It's a button that when clicked, will show the search form. The form doesn't show on iPhone. Also when I add $('#showSearch').remove(); in there, it doesn't get removed, even if I add a function as the second parameter of show() it still doesn't hide the button.
Thanks for your help.
jQuery Docs:
http://api.jquery.com/show/ (Shows)
http://api.jquery.com/hide/ (Hides)
http://api.jquery.com/remove/ (Deletes)
http://api.jquery.com/toggle/ (Show/Hide)
Live Example:
http://jsfiddle.net/qQnsj/1/
JS
$('#viewMeButton').click(function() {
$('#viewMe').toggle(); // used toggle instead of .show() or .hide()
});
$('#removeMeButton').click(function() {
$('#removeMe').remove();
});
HTML
<div data-role="page" id="home">
<div data-role="content">
<div id="viewMe">
Hello I'm in the div tag, can you see me?
</div>
<br />
<button id="viewMeButton">Show / Hide</button>
<br />
<div id="removeMe">
Click the button to remove me
</div>
<br />
<button id="removeMeButton">Remove Me</button>
</div>
</div>
I know this is an old question, but I recently had a similar problem. Hopefully this will help someone else if they find this question, too.
I was trying to hide a set of ui-block-* elements in a JQM grid. On the same grid cells, my media query for the iPhone was setting display: block !important. That was winning.
The !important directive was unnecessary in my case and when I removed it the calls to hide() and show() began working as expected.

How to select tinyMCE textarea when pressing tab key?

Hello
I got similar to that code
<form method="post" action="/" name="form" autocomplete="off">
<input type="text" name="title" />
<textarea name="body"></textarea>
<input type="text" name="tags" />
<input type="submit" name="submit" value="Send" />
</form>
Where the "textarea" is tinyMCE... problem comes when I try to move selection through input and textarea items.
When the page is loaded it focus input[name=text] but when I press Tab key it select input[name=tags] instead textarea[name=body] (which is next)
Can you assist me with that issue ?
This is impossible with conventional tab indexes as TinyMCE actually hides the <textarea> and creates an <iframe> with the editor in it. You could make a work around by forcing focus on the editor in the blur event of the previous element in javascript, here is ajQuery snipped of the top of my head:
$('#prev-input').blur(function(){
tinyMCE.get('textarea').focus();
});
Then as soon as the user leaves the previous input focus would jump to the TinyMCE editor, you could possibly add a blur() method as well to got to the next element.
I resolved this with adding 'tabfocus' to plugins and then add tabfocus_elements : ":prev,:next"
http://tinymce.moxiecode.com/wiki.php/Plugin:tabfocus
http://tinymce.moxiecode.com/wiki.php/tabfocus_elements
There is a plugin solved it
http://sourceforge.net/tracker/index.php?func=detail&aid=1657397&group_id=103281&atid=738747
I known it from TinyMCE forum
http://tinymce.moxiecode.com/forum/viewtopic.php?id=813
I needed a custom tabbing order from another element on the page. The code I used to resolve this in IE11 was
var button = $('#btn-id');
button.on('keydown', function (e) {
if ((e.which === 9 && !e.shiftKey)) {
e.preventDefault();
$(tinyMCE.get()[0].contentWindow).focus();
}
});
Be aware that the above code will only work if you've only got one editor on the page. Otherwise you'll have to cycle through the array returned by tinyMCE.get() to find the correct editor to set focus to.
I found out that the focus event is being triggered but focussing in the element (container) didn't work all the time.
In my case I tabbed from editor 1 - 4 and when I shift+tab I lost focus on the instance.
To fix this I added the following to the setup event of tinyMCE
setup: function(editor) {
editor.on("focus", function(e) {
e.target.editorContainer.scrollIntoView();
});
}