I am working with a chatbot interface and I need to retrieve the message id and add it to the submit function. I attempted to use $(".msf-text").id, but it returned undefined. How can I retrieve the id? It is important to note that there are multiple messages within the msg-text class, and I would like the submit function to contain the id of the submitted message.
randomeIndex = getRandomInt(100000000)
message_index = randomeIndex
<div class="msg-text" id="message-${message_index}">${text}</div>
msgerForm.addEventListener("submit", event => {
const message_id = $(".msf-text").id
console.log("message id is " + message_id)
}
2
Answers
Use
jQuery::prop()
method to access a DOM property of the first element in a jQuery object:https://api.jquery.com/prop/
(you made a type also
.msf-text
=>.msg-text
)Or (make sure the element exists):
You may consider not using jQuery. Here’s a minimal reproducable example
to retrieve the last element of a collection of specific elements.