5

I'm working through David's tutorial on Meteor at http://meteortips.com/.

How do I insert an integer instead of string on form submit?

I think the following line needs to clarify that it's an integer, but I'm not sure how.

var playerScoreVar = event.target.playerScore.value;

Here's my whole code.

 Template.addPlayerForm.events({

  'submit form': function(event){
      event.preventDefault();
      var playerNameVar = event.target.playerName.value;
      var playerScoreVar = event.target.playerScore.value;
      PlayersList.insert({
          name: playerNameVar,
          score: playerScoreVar,
      });
      event.target.playerName.value = ""
      event.target.playerScore.value = ""
  }
});

1 Answer 1

4

Just convert it to an integer prior to the insert:

var playerScoreVar = parseInt(event.target.playerScore.value, 10);

or

var playerScoreVar = Number(event.target.playerScore.value);

You can see the differences explained here;

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, what does the 10 mean on the end of that line? And why can't I just write 'Int' somewhere in that line and be done with it? :D
It's the radix (base). See the docs.
Got it, so I either have to parseInt or make sure they're only putting numbers in the form element in the first place. Thanks.
Whenever you extract the value from an input it will be a string, so you'll have to do the conversion. And yes, you probably want some other kind of form validation on top of this.

Your Answer

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.