I am not receiving the data that i place in the input fields… since the event.preventDefault(); ain’t working for some reason any suggestions?
Are you getting an error in the Console ?
Same problem here. When I run :
var fh = new App.FormHandler('[data-coffee-order="form"]');
fh.addSubmitHandler();
to get the seralized array, I get the console message (“setting submit handler from form”), but then it doesn’t prevent de default behaviour of “submit”. Submit button just reloads the page. I get no error message.
This is my formhandler.js
(function(window) {
'use strict'
var App = window.App || {};
var $ = window.jQuery;
function FormHandler(selector) {
if (!selector) {
throw new Error('No selector provided')
}
this.$formElement = $(selector);
if (this.$formElement.length === 0) {
throw new Error('Could not find element with selector: ' + selector);
}
}
FormHandler.prototype.addSubmitHandler = function() {
console.log('Setting submit handler from form');
this.$formElement.on('submit', function(event) {
event.preventDefault();
var data = $(this).seralizeArray();
console.log(data);
});
};
App.FormHandler = FormHandler;
window.App = App;
})(window);
and this is my index.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>coffeerun</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css">
</head>
<body class="container">
<header>
<h1>CoffeeRun</h1>
</header>
<section>
<div class="panel panel-paneldefault">
<div class="panel-body">
<form data-coffee-order="form">
<div class="form-group">
<label for="coffeeOrder">Coffee Order</label>
<input class="form-control" name="coffee" id="coffeeOrder" autofocus>
</div>
<div class="form-group">
<label for="emailInput">Email</label>
<input class="form-control" name="emailAddress" id="emailInput" value="" placeholder="what@fuq.com">
</div>
<div class="radio">
<label>
<input type="radio" name="size" value="short">
Short
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="size" value="tall" checked>
Tall
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="size" value="grande">
Grande
</label>
</div>
<div class="form-group">
<label for="flavorShot">Flavor Shot</label>
<select id="flavorShot" class="form-control" name="flavor">
<option value="">None</option>
<option value="caramel">Caramel</option>
<option value="almond">Almond</option>
<option value="mocha">Mocha</option>
</select>
</div>
<div class="form-group">
<label for="strengthLevel">Caffeine Rating</label>
<input type="range" name="strength" id="strengthLevel" value="30">
</div>
<button type="submit" class="btn btn-default">Submit</button>
<button type="reset" class="btn btn-default">Reset</button>
</form>
</div>
</div>
</section>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js" charset="utf-8"></script>
<script src="scripts/datastore.js" charset="utf-8"> </script>
<script src="scripts/truck.js" charset="utf-8"></script>
<script src="scripts/formhandler.js" charset="utf-8"></script>
<script src="scripts/main.js" charset="utf-8"></script>
</body>
</html>
Thanks!
EDIT: I had two typos: “serializedArray” had one and I was writing the argument for the constructor wrong. All is good now
Same problem. Don’t really know how to solve it