0

As according to the advice at Prevent form redirect OR refresh on submit? , I have my form which is

<form id="editingForm">
 Line height (between 10 and 60): 
 <input type="number" id="LineHeightEntry" name="LineHeightEntry" min="10" max="60" value="30">
 <input type="submit" id="submitLineChange" value="Submit">
</form>

In a file called test.html. The javascript is

$('#editingForm').submit(function(){
 alert("abc");
 return false;
});

The intent is to have the function be called, and then javascript can do something to the page, but the page is not reloaded or redirected elsewhere. However, instead what I get is say I set LineHeightEntry to 40 and hit submit. Then it redirects to test.html?LineHeightEntry=40. Why does it do that?

edit - The file is at http://probuling.net/sandbox/test.html

asked Feb 20, 2014 at 15:11
5
  • 1
    are you programmatically setting action attribute in your form...as I dont see it Commented Feb 20, 2014 at 15:16
  • need to set method attribute for form too... Commented Feb 20, 2014 at 15:16
  • 1
    It doesn't redirect: jsfiddle.net/3RAPj Commented Feb 20, 2014 at 15:17
  • You do have jQuery loaded, right? Commented Feb 20, 2014 at 15:18
  • I do, and even the suggestion with replacing the jquery with non jquery still redirects. Commented Feb 20, 2014 at 15:27

4 Answers 4

1

Here is a working example:

<html>
<head>
<script src="jquery-1.9.1.min.js"></script>
</head>
 <form id="editingForm" action="toto">
 Line height (between 10 and 60): 
 <input type="number" id="LineHeightEntry" name="LineHeightEntry" min="10" max="60" value="30">
 <input type="submit" id="submitLineChange" value="Submit">
</form>
<script>
$('#editingForm').submit(function(event)
{
 alert("abc");
event.preventDefault(); // if you want to disable the action
 return false;
});
</script>
</html>
Satpal
134k13 gold badges168 silver badges171 bronze badges
answered Feb 20, 2014 at 15:28
Sign up to request clarification or add additional context in comments.

6 Comments

@user2963178 I think the problem was that you were calling that js before the DOM was loaded. Therefore, you were trying to attach an event to an element that didn't exist yet.
Well this example works, but when I added the event.preventDefault() into the script on my main page at probuling.net/sandbox/test.html it still redirects... and jquery is included so I'm still as confused as before. It didn't help that it actually took about 5 minutes of ftping before ipage would stop replacing my file with a blank file lol...
and when it redirects it redirects to an old version of the page but of the same name... which does not have jquery, sometimes has different text on the page, and has an old css file which is no longer included.
Patrick Q is right, you need to put the Javascript binding code AFTER your html form. I just did it, it works perfectly
Ah I didn't understand what he meant. Yes now it works with that change.
|
0

Add action attribute to the form or you are just refreshing the page on submit.

<form id="editingForm" action="/another/url">

I would recommend learning how to use normal form submit before even try using Javascript. Any input with name attribute will be appended to the URL since you have no action and default method is set to GET.

answered Feb 20, 2014 at 15:17

3 Comments

That's not the issue. By default the page would post back on itself.
I know how to use normal form submit. What I want is for it to not refresh the page or load any other page. Basically the input fields let the user modify the CSS of some elements on the page, and a textarea lets the user modify the contents of those elements as well. So when the form is submitted I want it to just run the javascript functions, which do all the work. I have it all working in the sense that I tied it to an onclick of a button, and so it works nicely with that. However I can't get it to work with the form, so that's what I'm interested in.
Submit function callback is working fine. Checkout the fiddle jsfiddle.net/ankur1990/935V6
0

remove line return false;

This prevent default event. Read more detail: http://fuelyourcoding.com/jquery-events-stop-misusing-return-false/

answered Feb 20, 2014 at 15:19

Comments

0

try doing it without jQuery. Also, try using the event.preventDefault

document.querySelector('#editingForm').onsubmit = function(event) {
 alert('abc');
 event.preventDefault();
 return false;
};
answered Feb 20, 2014 at 15:22

2 Comments

that doesn't work either. I'm putting the url of the file up in the OP. probuling.net/sandbox/test.html
I just tried your test page and it works as expected for me (on Chrome). What browser are you using? Did you disable javascript?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.