4 Replies - 1232 Views - Last Post: 12 August 2015 - 02:53 PM

#1 b3tac0d3   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-August 15

preventDefault() not working on a login form

Posted 11 August 2015 - 08:09 PM

Hey All - I have used this method a million times but I can't seem to get it working. Instead of preventing default and giving me a message, it just redirects to the check_login.php page. I have tested the simple things; Popped an alert out, checked some of the form stuff but this particular piece of code doesn't want to work for me. I am running the code in a ColorBox if anyone is familiar with that but I don't think that should have any direct affect. I am also referencing library 2.1.4 and I have verified that jQuery is working in general, on other parts of my site. Any thoughts? Thanks!

jQuery
<script type = 'text/javascript'>
		$('document').on('submit', '#frmLogin', function(e){
			e.preventDefault();
			alert('submitted');
		});
</script>



HTML
<form class = 'Frm-cb' id = 'frmLogin' action = 'http://localhost/mgo/modules/login/php/check_login.php'>
	<h1>Welcome Back</h1>
	
	<label>email</label>
	<input type = 'text' name = 'loginEmail' id = 'loginEmail'>
	
	<label>password</label>
	<input type = 'password' name = 'loginPass' id = 'loginPass'>
	
	<div class = 'checkHolder'>
		<input type = 'checkbox' name = 'loginRemember'> <a>remember me</a>
	</div>
	
	<div class = 'checkHolder'>
		<input type = 'checkbox' name = 'loginCookie'> <a>stay logged in</a>
	</div>
	
	<button type = 'submit'>go</button>
	
	<a href = '#'>Forgot password</a>
</form>



Is This A Good Question/Topic? 0
  • +

Replies To: preventDefault() not working on a login form

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5612
  • View blog
  • Posts: 14,686
  • Joined: 18-April 07

Re: preventDefault() not working on a login form

Posted 11 August 2015 - 10:50 PM

Don't use $('document') use $(document). Notice that there is no single quotes. I believe this should fix your problem. :)
Was This Post Helpful? 1
  • +
  • -

#3 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: preventDefault() not working on a login form

Posted 11 August 2015 - 11:33 PM

I would prefer to skip the event delegation:
$('#frmLogin').on('submit', function(e){

because, even though the original probably works, it just looks wrong to me because document doesn't have a submit event. (I would test the original in different browsers as well, presumably you have.)
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4303
  • View blog
  • Posts: 13,677
  • Joined: 08-June 10

Re: preventDefault() not working on a login form

Posted 12 August 2015 - 12:40 AM

Quote

because, even though the original probably works, it just looks wrong to me because document doesn't have a submit event.

I’d bring up another reason. the target is an element identified by an ID. additionally, that element is most like there from the beginning.

regarding document and the submit event, the event eventually bubbles up and reaches document. whether a submit event makes sense on a document doesn’t matter technically.
Was This Post Helpful? 1
  • +
  • -

#5 b3tac0d3   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-August 15

Re: preventDefault() not working on a login form

Posted 12 August 2015 - 02:53 PM

Thanks for the feedback, guys. I must have been more tired than I thought because it turned out to be the quotes around the ('document') but I am going to look in to making this a little cleaner with everything else mentioned here. Thanks again!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1