I have this code to respond to a form submission on a template:
Template.addJobLoc.events({
'submit form': function(event) {
event.preventDefault();
var username = Session.get("username");
var jobloc = event.target.textJobLoc.val; // <= This is "undefined"...???
Meteor.call('insertJobLocationData', username, jobloc, function(err) {
if (err) {
Session.set("lastErrMsg", "insertJobLocationData failed");
} else {
alert(jobloc + ' inserted');
$('#textJobLoc').val("");
$('#textJobLoc').focus();
} // else
}); // Meteor call
} // submit form event
});
When I step through it in CDT after entering a value in the âtextJobLocâ text input element and clicking the
"Save Job/Location" submit input âbuttonâ, the jobloc values is âundefinedâ
I donât see why they should be - I have entered a value in the text input. Here is the template:
Add a Job/Location
Log In
Designation for Job/Location
So why are the values on my form undefined when I select the submit button? Is âvalâ the wrong value to grab?
event.target returns a reference to the object that dispatched the event.
Would try the following
Template.addJobLoc.events({
'submit form': function(event,template) {
event.preventDefault();
var username = Session.get("username");
var jobloc = template.find('#textJobLoc').value;
Meteor.call('insertJobLocationData', username, jobloc, function(err) {
if (err) {
Session.set("lastErrMsg", "insertJobLocationData failed");
} else {
alert(jobloc + ' inserted');
$('#textJobLoc').val("");
$('#textJobLoc').focus();
} // else
}); // Meteor call
} // submit form event
});
I tried your suggestion:
var jobloc = template.find('#textJobLoc').value;
âŚbut that fails, going to template.js
Maybe you didnât mean literally âtemplate.â but I donât know what to substitute.
I also tried:
('#textJobLoc').value;
âŚbut I get âundefinedâ with that, too.
Hi,
I forgot to mention I added template as a second param in the function âsubmit formâ: function(event,template) {}
Y old $(â#textJobLocâ).val(); should also do the trick btw
I suggest reading thru these aswel
http://docs.meteor.com/#/full/eventmaps
http://docs.meteor.com/#/full/template_inst
1 Like