0

I have this div in which I add more divs with javascript.

However, everytime I add a new div the to div with javascript, it refreshes so for example a youtube video in one of those divs will stop playing.

Can I put these divs into the div without reloading it?

My current code for putting in thing is

m += "new thing <a> and other stuff </a>"

I NEED it to put the new thing I want without reload. I currently put them in using href="javascript: addMessage('current time', 'user', 'message')"

The addMessage code:

function addMessage(time, user, msg) {
if (msg == "") {
    return false;
}
var m = document.getElementById('message-panel');
m.innerHTML += "<div class='sentMessage'><span class='time'>" + time + "</span><span class='name'><a>" + user + "</a></span><span class='message'>" + msg + "</span></div>";
pageScroll();
if (user != "SERVER") {
    if (user != "ERROR") {
        playAudio('new-message-sound');
    }
}
return false;

}

My only solution to putting new messages in if with href="javascript:addMessage()". I CAN NOT DO ONCLICK="" because I'm using java to controll the javascript!

My javacode for putting in the messages:

public void addMessage(String user, String msg) {
    try {
        getAppletContext().showDocument(new URL("javascript:addMessage(\"" + Time.now("HH:mm") + "\", \"" + user + "\", \"" + msg + "\")"));
    }
    catch (MalformedURLException me) {}
}

Thanks in advance, enji

1 Answer 1

2

Create the element with document.createElement('tag name here');, then insert it with m.appendChild(newelement);. This leaves any elements before it unaffected.

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

2 Comments

So i do var n = document.createElement("<div class='sentMessage'><span class='time'>" + time + "</span><span class='name'><a>" + user + "</a></span><span class='message'>" + msg + "</span></div>"); m.appendChild(n) ??
No, you do var n = document.createElement('div'); n.className = 'sentMessage'; n.innerHTML = "<span class='time'>.........</span>"; m.appendChild(n);

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.