0
<!DOCTYPE html>
<html>

<head><style>
body, html{
    margin: 0;
    padding: 0;
    height: 100%;
    width: 100%;
    position: relative;
}
.drag_box{
    width: 40%;
    height: 90%;
    border: black 1px solid;
    background-color: lightgray;
    margin-top: 20px;
}
.drag_box h2{
    text-align: center;
}
.content{
    width: 80%;
    overflow: auto;
    border: solid 2px black;
    background-color: burlywood;
}
</style>

</head>
<body>
<script>
function allowDrop(ev) {
    ev.preventDefault();
}

function drag(ev) {
    ev.dataTransfer.setData("text", ev.target.id);
}

function drop(ev) {
    if(document.getElementById(ev.target.id).id.includes("content")){
        return;
    }
    else{ 
    ev.preventDefault();
    var data = ev.dataTransfer.getData("text");
    ev.target.appendChild(document.getElementById(data));
    }
}
</script>

I created this function to use in a php script so it could take announcements from my school's website and automatically create content on the page. However, when run, the code does not execute at all. When the script tags are palced in the 'announcements' div, the code will execute and create the content.

<script>
function addContent(id, headline, content){

    var announcementContent = document.createElement('div');

    announcementContent.setAttribute('class', 'content');
    announcementContent.setAttribute('id', 'content'+id);
    announcementContent.setAttribute('draggable', 'true');
    announcementContent.setAttribute('ondragstart', 'drag(event)');
    announcementContent.innerHTML = '<h3>'+ headline +'</h3><p>'+ content +'</p>';

    document.getElementById('announcements').appendChild(announcementContent);


}
    addContent('3','test','testest');
</script>

<div id="announcements" class="drag_box" style="float:left; margin-left: 9%;" ondrop="drop(event)" ondragover="allowDrop(event)">
    <h2>Announcements</h2>
</div>
<div id="delete_column" class="drag_box" style="float:right; margin-right: 9%;" ondrop="drop(event)" ondragover="allowDrop(event)">
    <h2>Trash Box</h2>
</div>
</body>
</html>
1
  • 1
    In the future, check for errors in the Javascript console before posting here. You need to perform some basic debugging on your own. Commented Aug 17, 2016 at 17:58

2 Answers 2

2

You're calling the function before the announcements DIV has been added to the DOM, so document.getElementById('announcements') can't find it. You should be getting an error in the Javascript console saying that you can't call .appendChild() on undefined.

Put the script after the DIV.

<div id="announcements" class="drag_box" style="float:left; margin-left: 9%;" ondrop="drop(event)" ondragover="allowDrop(event)">
    <h2>Announcements</h2>
</div>
<div id="delete_column" class="drag_box" style="float:right; margin-right: 9%;" ondrop="drop(event)" ondragover="allowDrop(event)">
    <h2>Trash Box</h2>
</div>
<script>
function addContent(id, headline, content){

    var announcementContent = document.createElement('div');

    announcementContent.setAttribute('class', 'content');
    announcementContent.setAttribute('id', 'content'+id);
    announcementContent.setAttribute('draggable', 'true');
    announcementContent.setAttribute('ondragstart', 'drag(event)');
    announcementContent.innerHTML = '<h3>'+ headline +'</h3><p>'+ content +'</p>';

    document.getElementById('announcements').appendChild(announcementContent);


}
addContent('3','test','testest');
</script>

Or wait until the document is loaded before calling the function:

window.onload = function() {
    addContent('3', 'test', 'testest');
}
Sign up to request clarification or add additional context in comments.

Comments

0

Move your script after the html markup, right before </body> tag, otherwise the announcements element is not there yet when your script runs:

<div id="announcements" class="drag_box" style="float:left; margin-left: 9%;" ondrop="drop(event)" ondragover="allowDrop(event)">
    <h2>Announcements</h2>
</div>
<div id="delete_column" class="drag_box" style="float:right; margin-right: 9%;" ondrop="drop(event)" ondragover="allowDrop(event)">
    <h2>Trash Box</h2>
</div>

<script>
function addContent(id, headline, content){

    var announcementContent = document.createElement('div');

    announcementContent.setAttribute('class', 'content');
    announcementContent.setAttribute('id', 'content'+id);
    announcementContent.setAttribute('draggable', 'true');
    announcementContent.setAttribute('ondragstart', 'drag(event)');
    announcementContent.innerHTML = '<h3>'+ headline +'</h3><p>'+ content +'</p>';

    document.getElementById('announcements').appendChild(announcementContent);


}
    addContent('3','test','testest');
</script>

</body>
</html>

1 Comment

Thank you so much! Can't believe it was that simple.

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.