Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-12-2012, 05:18 PM   PM User | #1
hubs
New to the CF scene

 
Join Date: Apr 2009
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
hubs is an unknown quantity at this point
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>)
hubs is offline   Reply With Quote
Old 03-12-2012, 05:29 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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
__________________

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.

Last edited by Philip M; 03-12-2012 at 05:32 PM..
Philip M is offline   Reply With Quote
Old 03-12-2012, 05:38 PM   PM User | #3
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,307
Thanks: 12
Thanked 204 Times in 204 Posts
DanInMa is on a distinguished road
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()
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is offline   Reply With Quote
Reply

Bookmarks

Tags
javascript, linebreak, return

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:32 AM.


Advertisement
Log in to turn off these ads.