# Hoe DOM-elementen te krijgen en onClick-functionaliteit te wijzigen

**URL:** https://meta.discourse.org/t/how-to-get-dom-elements-and-change-onclick-functionality/238786
**Category:** Development
**Created:** [12 september 2022 om 14:47 UTC](https://meta.discourse.org/t/how-to-get-dom-elements-and-change-onclick-functionality/238786 "2022-09-12T14:47:48Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Sushanth\_Ps](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sushanth_ps/32/270505_2.png) [@Sushanth\_Ps](https://meta.discourse.org/u/Sushanth_Ps)
#### Post date: [12 september 2022 om 14:47 UTC](https://meta.discourse.org/t/how-to-get-dom-elements-and-change-onclick-functionality/238786/1 "2022-09-12T14:47:48Z")

</div>

Hey,

Any idea on how I can get DOM elements on discourse without having to use a setTimeout and a querySelector.  
Cause currently I do something like this:-

```plaintext
setTimeout(() => {
                    var createTopicButton = document.querySelector("#create-topic");
                    if(createTopicButton)
                        createTopicButton.onclick = function (e) {
                        if (window.Moengage && window.Moengage.track_event) {
                            window.Moengage.track_event("Topic_Create_Clicked", {});
                        }
                        setTimeout(() => {
                            var topicCreate = document.querySelector("#reply-control .save-or-cancel .btn-primary")
                            topicCreate.onclick = function (e) {
                                let heading = document.querySelector('#reply-control #reply-title').value;
                                let category = document.querySelector('.select-kit-header-wrapper>.select-kit-selected-name>.name>.badge-wrapper>.badge-category>.category-name')?.innerText;
                                let desc = document.querySelector('.d-editor-textarea-column>.d-editor-textarea-wrapper>textarea').value;
                                let imagePresent = desc.includes("upload://");
                                if (window.Moengage && window.Moengage.track_event && heading && category && desc) {
                                    window.Moengage.track_event("Topic_Created", {"Category":category,"Topic Name":heading,"Topic Description":desc,"Is_image_present":imagePresent});
                                }

                            }
                        },[300])
                        
                    };
                },[500])

```

If I don’t use the above timeouts, I sometimes get null elements as the DOM hasn’t loaded fully, even after putting this inside a window.onload function.  
so my objective is to get DOM elements and change the onChange functionality.

Any help in this would be great.

Thanks 🙂

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [12 september 2022 om 14:49 UTC](https://meta.discourse.org/t/how-to-get-dom-elements-and-change-onclick-functionality/238786/2 "2022-09-12T14:49:58Z")

</div>

Discourse is a full EmberJS application, so you can’t query/manipulate elements like if it was a server rendered plain HTML website.

In order to customize the web app you can follow the guidelines at

[Developer’s guide to Discourse Themes](https://meta.discourse.org/t/developer-s-guide-to-discourse-themes/93648)

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [13 september 2022 om 00:23 UTC](https://meta.discourse.org/t/how-to-get-dom-elements-and-change-onclick-functionality/238786/3 "2022-09-13T00:23:34Z")

</div>

On addition to looking at the developer guide, you might take a step back and describe what you are trying to do rather than your suggested solution.

---

<div class="post-metadata">

### Author: ![Sushanth\_Ps](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sushanth_ps/32/270505_2.png) [@Sushanth\_Ps](https://meta.discourse.org/u/Sushanth_Ps)
#### Post date: [13 september 2022 om 08:54 UTC](https://meta.discourse.org/t/how-to-get-dom-elements-and-change-onclick-functionality/238786/4 "2022-09-13T08:54:46Z")

</div>

Hey Jay,

So I was trying to call moEngage analytic functions onClick of certain buttons on the page (eg:- like,comment,share)

I can only do this if I have access to the onClick functionality of these buttons. Is there any plugin within discourse that would help me achieve this?

Thanks

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [13 september 2022 om 09:56 UTC](https://meta.discourse.org/t/how-to-get-dom-elements-and-change-onclick-functionality/238786/5 "2022-09-13T09:56:20Z")

</div>

You should be able to do that in a theme component. I recommend that you start with the developer’s guides to see how things work.
