Nathaniel Hill
Nathaniel Hill

Reputation: 49

Why is line break being written as a string?

I am trying to write a program that submits text on my website and I am having a lot of trouble trying to do a line break after the text because break line is being written as a string.

var main = function() {
    var today = new Date();
    var month = parseInt(today.getMonth() + 1);
    var day = parseInt(today.getDate());
    var year = parseInt(today.getFullYear());   


    $(".btn").click(function() {
        var post = $(".status-box").val();

        $("<li>").text(post + "<br>" + month + "/" + day + "/" + year).prependTo(".posts");


        $(".status-box").val("");
        $(".counter").text("140");
        $(".btn").addClass("disabled");
    });

    $(".status-box").keyup(function() {
        var postlength = $(this).val().length;
        var charactersLeft = 140 - postlength;
        $(".counter").text(charactersLeft);


        if (charactersLeft < 0) {
            $(".btn").addClass("disabled");   
        } else if (charactersLeft === 140) {
            $(".btn").addClass("disabled");
        } else {
            $(".btn").removeClass("disabled");
        }

    });
    $(".btn").addClass("disabled");

};

$(document).ready(main);

Upvotes: 1

Views: 61

Answers (1)

Leo
Leo

Reputation: 14820

You are using .text() which will ALWAYS html-encode the text provided...use .html() instead

$("<li>").html(post + "<br>" + month + "/" + day + "/" + year).prependTo(".posts");

Upvotes: 4

Related Questions