# How to get DOM elements and change onClick functionality

**URL:** <https://meta.discourse.org/t/how-to-get-dom-elements-and-change-onclick-functionality/238786>\
**Category:** Development\
**Created:** [September 12, 2022, 2:47pm 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:** 1\
**Showing post:** 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:** [September 12, 2022, 2:47pm 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 🙂

---

_[View the full topic](https://meta.discourse.org/t/how-to-get-dom-elements-and-change-onclick-functionality/238786)._
