Why is this value on my form undefined?

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