yash sachdeva
yash sachdeva

Reputation: 636

how do i access the document dom object in ejs?

I am trying show a list of participants when the user clicks on the button.But each time i end up with an error ""document is not defined".(Please don't give me jquery!!).

<% var bt = document.getElementById("bt");
        bt.addEventListener('onclick',function(){
    var chatbox = document.getElementsByClassName('parti');
    var msg = document.createElement('div');
    msg.setAttribute('class', 'participants');
    msg.textContent('Participant \n\n\n'); %>
<%= chatbox.appendChild(msg); %>
<% }); %>

Upvotes: 3

Views: 4936

Answers (1)

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85593

Change this line:

bt.addEventListener('onclick',function(){

With this:

bt.addEventListener('click',function(){

When we use addEventListener we don't need to use prefix 'on' for even name.

Also, you have used getElementsByClassName and for this you need to iterate over array, so use:

<%= chatbox[0].appendChild(msg); %>

Upvotes: 1

Related Questions