Reputation: 1406
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
Upvotes: 0
Views: 104
Reputation: 4271
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;
Upvotes: 2