0

I have the following code inside a bootstrap modal - the data is being pulled from a button (that triggers the modal)

<script> 
  $('#myFavorites').on('show.bs.modal', function (event) { 
    // Heart Icon triggers 
    var heart = $(event.relatedTarget) 
    // Button that triggered the modal 
    // List of variables 
    var user = heart.data('user') 
    var title = heart.data('track') 
    var artist = heart.data('artist') 

    // Print to screen 
    var modal = $(this) modal.find('.modal-title').html('User: ' + user + '<p>Song: ' + title + '</p><p>Artist: ' + artist) 
    // modal.find('.modal-body input').val(title) 
  }) 
</script>

This all prints out on one line. How can I format it like so (with simple breaks to a new line):

User: 01
Song: Title Here
Artist: Artist Name Here
Source: URL Here
Poster: Poster Here

I injected the \n in several places but it broke the code?

3
  • <br>? <pre>? Commented May 25, 2017 at 21:20
  • Use Bootstrap's output formatting classes like col-md3, or create an HTML table. Commented May 25, 2017 at 21:25
  • As stated below - the HTML is not being recognized or a simple BR would work, as others have suggested. So even though this is a great suggestion, if a BR doesn't work, then table tags wont... hmmm Commented May 25, 2017 at 21:32

1 Answer 1

2

Use <br/> for new line break and use html function instead of text function:

modal.find('.modal-title').html('User: ' + user + '<br/>Song: ' + title + '<br/>Artist: ' + artist + '<br/>Source: ' + source + '<br/>Poster: ' + poster)
Sign up to request clarification or add additional context in comments.

9 Comments

I tried that but the output is: User: 2<br/>Song: After Midnight<br/>Artist: Blink 182<br/>Source: youtu.be/H86730HjLVA<br> --- the breaks are not recognized...?
While XHTML syntax (<br />) is legal, there really is no good reason to use it unless you will need to treat your HTML as XML (which these days would be very rare). If anything, it causes confusion and results in users using that self-terminating syntax in places where they shouldn't (i.e. <script src="..." />).
Are you using html function instead of text? @Davo
Here is the complete script:
<script> $('#myFavorites').on('show.bs.modal', function (event) { // Heart Icon triggers var heart = $(event.relatedTarget) // Button that triggered the modal // List of variables var user = heart.data('user') var title = heart.data('track') var artist = heart.data('artist') // Print to screen var modal = $(this) modal.find('.modal-title').text('User: ' + user + '<p>Song: ' + title + '</p><p>Artist: ' + artist) // modal.find('.modal-body input').val(title) }) </script>
|

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.