...

View Full Version : Line Break In JavaScript



hubs
03-12-2012, 06:18 PM
I have these line of javascript:



$(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:

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

Philip M
03-12-2012, 06:29 PM
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

DanInMa
03-12-2012, 06:38 PM
$("#answer").text("Your Name Is: " +nick);
$("#answer").fadeIn("slow");

to



$("#answer").html("Your Name Is: " +nick+"<br>);
$("#answer").fadeIn("slow");



+"<br>" won't work with .text(), you need to use .html()



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum