I have a social chatbox for my members. it is visible only on page1 .
I am trying to write code for the onMessageSent event. I am on the masterPage.js and wrote the following:
$w('#myChatbox').onMessageSent((message) => {
const channelId = message.channelId;
const messageText = message.payload.text;
});
But get the message that #myChatbox is not a valid selector.
I checked on the chatbox API page and I can’t find anything missing. The selector is in the correct case. What am I doing wrong?
Hi haggiben.
Try to copy the code you wrote, and past it inside the onReady function
$w.onReady(function() {
//Your code here
});
Hope this helps
kind regards,
Kristof.
I never really worked with chatboxes, but i will try to help where i can.
few questions.
-
The masterPage.js is this a file you created yourself?
-
did you copy the onReady function from the page code to your .js file?
-
.js file is a code file not directly linked to your page, wich means you can’t access your elements in there directly.
-
normaly there is no need to have an onReady function in your .js file. you write functions in there, and in your page you can import those files and use them.
the masterPage is an automatic js file wix added to Globle site.
Where else am I supposed to put the chat code?
I found this post
[https://www.wix.com/corvid/forum/community-discussion/how-to-access-wix-chat-chatbox-api-events](post
https://www.wix.com/corvid/forum/community-discussion/how-to-access-wix-chat-chatbox-api-events)
don’t know if its the same issue, but seems like they say it aint working in the preview site but it does work in the live website, maybe you can ty it out even with the error.
Thank you! You are absolutely right,
-
I placed the code in the onReady Function on masterPage.js and ignored the error.
-
I clicked on Preview (top left corner of the dashboard). Sent a chat message. Worked like a charm.
Glad i could help you out 