I have a script that randomly generates a room id when joined. I want to copy that ID with a click of a button. It would be easy job with a input element, however, I don't know how to even target that random ID to edit and manipulate it. How could I do that?
function createRoom(){
var option = document.createElement("option");
option.text = makeid(10);
roomSelection.add(option);
window.sessionStorage.setItem("arhostas", 1);
}
function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
var roomUrl = document.createElement("div");
roomUrl.innerText = `Room id: ${room}`
roomID.append(roomUrl)
<div id='roomId'></div>
Room ID is already generated when joined to the room. I just need to copy it to the clipboard somehow. Adding a "createRoom" code.

room_id_divis not a valid html-element. You can create an element and assign some properties to it in one go usingObject.assign. If you want to use the id within theinnerTextof the created element, you'll have to create the (random) id separately.Alternatively you can set the content of a div using a pseudo-selector in css (
:before). In that case you don't need to create a random id separately. The downside is that the text within the div can not be selected (hence, not copied to the clipboard).Both demonstrated in this snippet: