0

How can I pass an attribute of div into it's own onclick function? For example, I have this div:

var join_button = document.createElement("div");
join_button.setAttribute("class", "join_button");
join_button.innerHTML = "join";
join_button.setAttribute("room_name", name);
join_button.setAttribute("onclick", "join_room()");

Now, in function join_room() I need to know the room name attribute of this join_button. Ofcourse, I have not only one join_button on my page, I dont know it's names and I need to handle all of them.

If I try to use this It tells me undefined is not a function

function join_room() { this.getAttribute("room_name"); }

undefined is not a function

0

1 Answer 1

2

You can use this to read the objects attribute.

var join_room = function() {
    var room_name = this.getAttribute('room_name);
}

then set the onclick like this.

join_button.onclick = join_room;

JSFIDDLE

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

1 Comment

Yes, I've tryed it, but I have an error on it: undefined is not a function

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.