-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.

2
  • You mean to listen to the enter event in a view? Oct 21, 2015 at 17:27
  • Yeah, i'm confused by that too. Please explain it further with an actual use case. Oct 21, 2015 at 17:45

1 Answer 1

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>

7
  • Also, I managed to find the answer. It's just like this: alert("Hello\nHow are you?"); :) Oct 22, 2015 at 9: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?, JavaScript string newline character?
    – Yura
    Oct 22, 2015 at 11:14
  • :) great. Now that you know the question, you can find the answer in all detail.
    – Yura
    Oct 22, 2015 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, 2015 at 19:41
  • 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, 2015 at 19:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.