Eduardo Serra
Eduardo Serra

Reputation: 5

My scripts with javascript do not work at all

I was writing a kind of blog publishing engine with javascript, but my code doesn't work! I beg you, please, tell me what's wrong with this code (I'M brazilian, so comments are in portuguese, but you can ignore them:

<!DOCTYPE html>
<html>
<head>
<style>
    .new {
        background-color: #DDDDDD;
          margin-top: 15px;
          margin-left: 30px;
          margin-right: 30px;
          display: none;
          width:600px;
          height:600px;
    }

    #entries{
        width:500px;
        height:500px;
        background-color:blue;
    }



    .newtwo{
        background-color: #DDDDDD;
        margin-top:15px;
        margin-left: 30px;
        margin-right:30px;
    }

    .inputs{
        padding-bottom: 30px;
        width : 500px;
        height:200px;
        overflow:scroll;
    }

    button{
        width:150px;
        height:100px;
        background-color:#3333CC;
        font-family:Impact;
        font-size: 25px;
        color: white;
        border-radius:15px;
        margin-left:30px;
        padding-top:15px;
    }

    </style>
    <script type="text/javascript">
    //crie o objeto Inserir, responsável pelo conteúdo inserido
    function Inserir (title, text, date) {
        //texto principal
        this.text = text;
        //data de publicacao
        this.date = date;
        //título da publicação
        this.title = title;
    }

    var date = new Date();
    var dateFormat = date.getDate() + "/" + (date.getMonth() + 1) + "/" + date.getFullYear();

    //cria o objeto insert
    var insert = new Inserir(document.getElementById("title").value, document.getElementById("body").value, new Date("13/10/2015"));

    document.getElementById("publish").onclick = function(){
        var entryText = document.createElement("p");
        //gere o texto formatado
        entryText.appendChild(document.createTextNode(document.getElementById("title").value));
        document.getElementById("entries").appendChild(entryText);
    }

</script>

</head>
<body>
<div id = "insert" class="new">
<form>
    <input type= "text" id="title" class="inputs" placeholder="Put title here"><br />
    <input type = "text" id="body" size="100" class="inputs" placeholder="write the main text here"><br />

</form>
</div>
<button id="add" onclick="document.getElementById('insert').style.display='block';">Add new entry</button>
<button id="publish" onclick="createEntry();"> Publish! </button>
<div id="entries"></div>
</body>
</html>

Upvotes: 0

Views: 49

Answers (1)

jeerbl
jeerbl

Reputation: 7867

Try to put all your script in a window.onload event like this:

window.addEventListener('load', function() {
  //Your javascript code goes here
}, false);

Upvotes: 1

Related Questions