0

I have this code and it isn't working the way i want. I want it to create a new div under the other one everytime the button is clicked, but it is just changing the content of the div. help

<!DOCTYPE html>
<html>
<head>
<style>
p{
background-color: red;
font-family: arial;
}
</style>
</head>
<body>

Name: <textarea id="myText" value=""></textarea>

<button type="button" onclick="myFunction()">Enviar</button>

<div id="demo"></div>

<script>
function myFunction() {
    var x = document.getElementById("myText");
    var defaultVal = x.defaultValue;
    var textoComentario = x.value;

    if (defaultVal == textoComentario) {
        alert("Digite um comentário!");
    } else {
           document.getElementById("demo").innerHTML =  "<p>" + textoComentario + "</p>";
        x.value = "";

    }
}
</script>

</body>
</html>
4

3 Answers 3

0

You can try with jquery more easily:

 $('<div/>').html(textoComentario).appendTo('#demo');

EDIT

You can make it with pure-javascript with createElement too:

http://www.w3schools.com/jsref/met_document_createelement.asp

Good luck!

Sign up to request clarification or add additional context in comments.

Comments

0

I think you are looking for this:

WORKING:DEMO

HTML

NO CHANGE

CSS

NO CHANGE

JS/JQuery

function myFunction() {
    var x = document.getElementById("myText");
    var defaultVal = x.defaultValue;
    var textoComentario = x.value;
    alert(textoComentario);

    if (defaultVal == textoComentario) {
        alert("Digite um comentário!");
    } else {
           $("#demo").css("display","block");
           $("#demo").append("<p>" + textoComentario + "</p>");
        x.value = "";

    }
}

Comments

0

Replace your

document.getElementById("demo").innerHTML =  "<p>" + textoComentario + "</p>";

into $('#demo').append("<p>" + textoComentario + "</p>");

It will work.......

Comments

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.