# chatbox not a valid selector

**URL:** <https://forum.wixstudio.com/t/chatbox-not-a-valid-selector/30502>\
**Category:** Ask the community\
**Tags:** wix-members-area, question, code\
**Created:** [October 22, 2020, 12:23pm UTC](https://forum.wixstudio.com/t/chatbox-not-a-valid-selector/30502 "2020-10-22T12:23:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![haggiben](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/haggiben/32/14952_2.png) [@haggiben](https://forum.wixstudio.com/u/haggiben)\
**Post date:** [October 22, 2020, 12:23pm UTC](https://forum.wixstudio.com/t/chatbox-not-a-valid-selector/30502/1 "2020-10-22T12:23:12Z")

</div>

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:

```javascript
$w('#myChatbox').onMessageSent((message) => {
 const channelId = message.channelId;
 const messageText = message.payload.text;
});

```

But get the message that #myChatbox is not a valid selector.

 ![](https://us1.discourse-cdn.com/wix/original/3X/6/b/6b770e0c99ddae5f26271ea28b2eb57eedd371bf.jpeg)

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?

---

<div class="post-metadata">

**Author:** ![volkaertskristof](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/volkaertskristof/32/18158_2.png) [@volkaertskristof](https://forum.wixstudio.com/u/volkaertskristof)\
**Post date:** [October 22, 2020, 12:42pm UTC](https://forum.wixstudio.com/t/chatbox-not-a-valid-selector/30502/2 "2020-10-22T12:42:48Z")

</div>

Hi haggiben.  
Try to copy the code you wrote, and past it inside the onReady function

```javascript
$w.onReady(function() {
//Your code here
});

```

Hope this helps

kind regards,  
Kristof.

---

<div class="post-metadata">

**Author:** ![haggiben](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/haggiben/32/14952_2.png) [@haggiben](https://forum.wixstudio.com/u/haggiben)\
**Post date:** [October 22, 2020, 12:54pm UTC](https://forum.wixstudio.com/t/chatbox-not-a-valid-selector/30502/3 "2020-10-22T12:54:46Z")

</div>

The error is still there

 ![](https://us1.discourse-cdn.com/wix/original/3X/6/3/63c3da521867fe3c3bf6fe22c55ca97ab24e7815.jpeg)

---

<div class="post-metadata">

**Author:** ![volkaertskristof](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/volkaertskristof/32/18158_2.png) [@volkaertskristof](https://forum.wixstudio.com/u/volkaertskristof)\
**Post date:** [October 22, 2020, 1:07pm UTC](https://forum.wixstudio.com/t/chatbox-not-a-valid-selector/30502/4 "2020-10-22T13:07:21Z")

</div>

I never really worked with chatboxes, but i will try to help where i can.  
few questions.

1. The masterPage.js is this a file you created yourself?

2. did you copy the onReady function from the page code to your .js file?

3. .js file is a code file not directly linked to your page, wich means you can’t access your elements in there directly.

4. 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.

---

<div class="post-metadata">

**Author:** ![haggiben](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/haggiben/32/14952_2.png) [@haggiben](https://forum.wixstudio.com/u/haggiben)\
**Post date:** [October 22, 2020, 2:20pm UTC](https://forum.wixstudio.com/t/chatbox-not-a-valid-selector/30502/5 "2020-10-22T14:20:26Z")

</div>

the masterPage is an automatic js file wix added to Globle site.

 ![](https://us1.discourse-cdn.com/wix/original/3X/f/e/fe483943a554886f103682664f1cc570bfb0575f.jpeg)

Where else am I supposed to put the chat code?

 ![](https://us1.discourse-cdn.com/wix/original/3X/6/4/6481ea56df98ed1ccec38059ef6534f5c46d91b6.jpeg)

---

<div class="post-metadata">

**Author:** ![volkaertskristof](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/volkaertskristof/32/18158_2.png) [@volkaertskristof](https://forum.wixstudio.com/u/volkaertskristof)\
**Post date:** [October 22, 2020, 2:53pm UTC](https://forum.wixstudio.com/t/chatbox-not-a-valid-selector/30502/6 "2020-10-22T14:53:30Z")

</div>

I found this post

[[https://www.wix.com/corvid/forum/community-discussion/how-to-access-wix-chat-chatbox-api-events](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](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.

---

<div class="post-metadata">

**Author:** ![haggiben](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/haggiben/32/14952_2.png) [@haggiben](https://forum.wixstudio.com/u/haggiben)\
**Post date:** [October 22, 2020, 10:45pm UTC](https://forum.wixstudio.com/t/chatbox-not-a-valid-selector/30502/7 "2020-10-22T22:45:53Z")

</div>

Thank you! You are absolutely right,

1. I placed the code in the onReady Function on masterPage.js and ignored the error.

2. I clicked on **Preview** (top left corner of the dashboard). Sent a chat message. Worked like a charm.

---

<div class="post-metadata">

**Author:** ![volkaertskristof](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/volkaertskristof/32/18158_2.png) [@volkaertskristof](https://forum.wixstudio.com/u/volkaertskristof)\
**Post date:** [October 23, 2020, 7:03am UTC](https://forum.wixstudio.com/t/chatbox-not-a-valid-selector/30502/8 "2020-10-23T07:03:13Z")

</div>

Glad i could help you out 🙂
