Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Line Break In JavaScript

    I have these line of javascript:


    Code:
    $(document).ready(function () {
        /*$('#first').focus();
        $(this).keyup(function (e) {
            if (e.keyCode == 13) {
                generate();
            }
        });*/
            });
    function generate() {
        if($("input[name='name']").val().length > 1) {
            fi = $("input[name='name']").val().substring(0,1).toUpperCase();
            var sl = ["Cool "];
            var sl2 = ["Hand "];
            var sl3 = ["Luke",];
     
            {
                var i = Math.floor(Math.random() * sl.length);
                var j = Math.floor(Math.random() * sl2.length);
                var k = Math.floor(Math.random() * sl3.length);
                var nick = sl[i] +   sl2[j] +   sl3[k];
            }
            {
            $("#answer").text("Your Name Is: " +nick);
            $("#answer").fadeIn("slow");
            } ;
        }
    }

    I would like for it to return:

    Your Name Is:
    Cool Hand Luke


    I want the "Cool Hand Luke" to be on a separate line but I don't know how to put a linebreak in the .text string. Is this something somebody could tell me how to do?

    Where in the line:
    Code:
    $("#answer").text("Your Name Is: " +nick);
    Do I put (<br>)

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by hubs View Post
    Do I put (<br>)
    Yes. + "<br>" But why not try it?
    This is not a line break in Javascript. It is a line break in the HTML text output.

    Quizmaster: A soliloquy is performed by how many people?
    Contestant: Four
    Last edited by Philip M; 03-12-2012 at 05:32 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    Code:
      $("#answer").text("Your Name Is: " +nick);
            $("#answer").fadeIn("slow");
    to

    Code:
      $("#answer").html("Your Name Is: " +nick+"<br>);
            $("#answer").fadeIn("slow");
    +"<br>" won't work with .text(), you need to use .html()


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •