-3

If I'm writing an alert in backbone.marionette how do I start a new line? E.g. when I use the alert() function (I think it's possibly a Javascript function rather than Backbone.Marionette; I'm just using it within Backbone.Marrionette code...) I currently have some code that looks like this:

alert("Some alert text " + variable + " some more alert text");

Which outputs:

"Some alert text [variable] some more alert text"

When I want it to output:

"Some alert text [variable]

Some more alert text"

NOTE: AT THE TIME OF WRITING THIS USER HAD BEGUN USING JAVASCRIPT AND BACKBONE.MARIONETTE AT THE SAME TIME AND WAS UNCLEAR AS TO THE DIFFERENCE. QUESTION LEFT AS IS OTHERWISE, AS THE ACCEPTED ANSWER MAY BE USEFUL.

Peter David Carter
  • 2,548
  • 8
  • 25
  • 44

1 Answers1

1

This might spark some idea. I will update this answer if you edit your question to make it more specific.

var Input = Backbone.View.extend({
  tagName: 'input',
  initialize: function(options){
    _.bindAll(this, 'onKeydown', 'onKeyup');
    this.$el.attr('type', options.type );
    this.$el.on('keydown', this.onKeydown);
    this.$el.on('keyup', this.onKeyup);
  },
  onKeydown: function(evt){
    /* prevent default action of 'enter' key */
    if( evt.keyCode === 13 ){
      evt.preventDefault();
    }    
  },
  onKeyup: function(evt){
    console.log( 'keyup', evt.keyCode, evt);
    /** if 'return' was pressed */
    if( evt.keyCode === 13 ){
      alert('You Entered: ' + this.$el.val() );
    }
  }
});

var input = new Input({
  type: 'text'
});

input.$el.appendTo( document.body );
<script src='http://code.jquery.com/jquery.js'></script>
<script src='http://underscorejs.org/underscore.js'></script>
<script src='http://backbonejs.org/backbone.js'></script>
Yura
  • 2,690
  • 26
  • 32
  • Also, I managed to find the answer. It's just like this: `alert("Hello\nHow are you?");` :) – Peter David Carter Oct 22 '15 at 09:49
  • Oh, so now when I see your answer, I can tell that your question was: "How do I insert a line break into a string in javascript?" Here's what you wanted to ask, I think - [How do I create a new line in Javascript?](http://stackoverflow.com/questions/5758161/how-do-i-create-a-new-line-in-javascript), [JavaScript string newline character?](http://stackoverflow.com/questions/1155678/javascript-string-newline-character) – Yura Oct 22 '15 at 11:14
  • :) great. Now that you know the question, you can find the answer in all detail. – Yura Oct 22 '15 at 19:28
  • "line-break in a non-string variable"? I have a hard time visualizing that. "non-string variable in an alert"? In an alert a non-string variable will be converted to a string, which might make little sense. – Yura Oct 22 '15 at 19:41
  • Say I wrote the following code: `var number = 1536742; alert("Your two part authentication number is " + number);` that would output: **Your two part authentication number is 1536742**. But supposing I wanted it to output: **Your two part authentication number is: 153 6742**. Would that be possible? – Peter David Carter Oct 22 '15 at 19:50
  • 1
    Yes, but what's the logic there? You will need to convert that number to a string first, and then insert `\n` to a position that you want, that might be after first three characters for example. – Yura Oct 22 '15 at 19:54
  • So, like `var number = num.toString();` and then you pass in a line break with '\n' using .splice()?? – Peter David Carter Oct 22 '15 at 20:01