Getting Enter key to submit a dojo.form - javascript

I have a form element I want to use in many forms, a standard submit button.
<div dojoType='dijit.form.Button' type='submit'
iconClass='dijitEditorIcon dijitEditorIconRedo'
style='float:left;margin:0 2px 00;padding:0;'
onClick=\"filter{$this->_grid->getId()}(); return false;\">Refresh
</div>
Current, this submit buttons works when clicked on, but the form does not submit when the enter key is pressed. Others have suggested a custom onkeypress event handler to submit the form when it catches the Enter key, but I'm hesitant because I don't want to break other forms on the page.

Stuff that you want running on form submission should be on form onsubmit, not a particular button.

Related

Is event of button which type is submit click or submit?

Could you please advise which event I should write in code for of type submit. Is it click or submit. I know that submit should be put for form. If button submit put inside in form I may write
$('form').submit()
is's for case of pushing button enter on keyboard.
If push button of the form should I write in code
$('button[type="sumbit"]').click()

How to prevent find :submit to enable rest of the buttons in the form?

Hello I have form that has an option to enable/disable some fields. If user selects No as an option for particular section of the form all fields in that form will be disabled. However, I use this logic when saving form data:
frmObject.find(":submit").prop("disabled", true); // Disable submit button
then this code to enable submit button:
frmMessage.show().addClass(obj.CLASS).html("Error!").delay(7000).fadeOut('slow').queue(function(){
$(this).removeClass(obj.CLASS).dequeue();
frmObject.find(":submit").prop('disabled', false); // Enable submit button
});
Problem that I have after form is submitted and successfully saved code that enables submit button will affect other buttons in the form that should remain disabled. I'm not sure why since the other buttons have <button></button> tag and they do not have type=submit. Does anyone know how to prevent this behavior?
I'm not sure why since the other buttons have tag
and they do not have type=submit. Does anyone know how to prevent this
behavior?
The default type for <button> is submit. It's a good idea to always specify the type explicitly:
<button type="button">I'm NOT a Submit Button</button>
<button>I'm AM a Submit Button</button>

Why isn't my JS pulling the value from the form properly?

HTML in question is pretty simple:
<form>
<input type="text" name="locSearch" id="locSearch" />
<button id="locSearchBtn"><i class="fa fa-search" id="search-icon"></i></button>
</form>
js:
$(document).ready(function() {
getsWeather('seattle, wa', 'f');
$("#locSearchBtn").click(function() {
getsWeather(document.getElementById('locSearch').value, 'f');
});
When I submit the form (either by pressing enter or by clicking the submit icon, the page reloads but with the default setting (i.e with 'Seattle, wa as the default argument for the getsWeather function). I need it to pull whatever is in the input box and use that as the argument in the getsWeather function but that currently isn't working.
Any ideas? Let me know if you need more of the code to understand it
If you want to modify the page, you need to prevent the form from being submitted when you click the button. Two ways to do that:
Add return false to the end of your click handler for the button. This will prevent form submission (if JavaScript is enabled on the client).
Add type="button" to the button so it's not a submit button anymore.
Ideally, you'd combine #1 with handling a form submission if the client doesn't have JavaScript enabled, to handle the small number of people who surf with JavaScript disabled via the form submission while handling JavaScript-enabled clients with the in-page update.

can i submit form with submit button and JS?

My question might be basic but i want it to clarify. i have one html form. it contain 2 submit button and some links. on click of one of the link i am trying to submit the form by java-script but not able to do so. but when i make all the button as normal button and submit the form by JavaScript then i am able to submit the form by link too.
Now my question is - if we have submit button in the form then can we submit the form by java-script too?
You don't need a submit button to submit a form - period. You can submit a form by javascript regardless of whether or not you have any fields or buttons at all, all you need is a form element.
My magic crystal ball tells me your form submits just fine, but your serverside script was written poorly and depends on the name/value of a submit button being sent.
Yes, you can, use the onsubmit event and then submit the form use javascript.
For example use jQuery:
$('#your_form').on('submit', function() {
if (/**some condition**/) {
$(this).submit();
}
return false;
});
If you submit your form with a link, i suppose you use this kind of code :
Submit my form
This is wrong because you submit the form and then you go on the link, you have to put "return false;" after your submit():
Submit my form

jquery redirect on enter press within a form?

I have an html form and within the form I have a <button> element. I am using jquery to redirect the page upon clicking the button (essentially I wanted to nest form elements but since its not valid xhtml I used a javascript workaround).
Furthermore, clicking the button grabs text from an input field, appends it to the query string then redirects the page (note that both the input field and the button are inside of the html form).
Now for what I want to do: I also want to have the same functionality when the user hits the 'enter' key from within the previously mentioned input field (i.e. same functionality as if the <button> was pressed. I have already written code that binds to the enter key (when I press enter in the input field I can get an alert to pop up). The problem is that since this input field is within <form> tags, I cannot seem to override the default action which is: upon pressing enter trigger the submit button. Is it even possible to override this and have the pressing enter event redirect the page to something other than whatever <form action is set to? Thanks.
Try
$('form').submit(function () {
return false;
});
This would really go against accessibility, but I think you could cancel the default action which is on the 'submit' event, with:
$('form#foo').submit(function(e) { e.preventDefault(); });
If I'm understanding correctly... or program that function to be dynamic and have it submit or not submit depending on a factor/flag.

Categories