Does ("document").ready Work in Unbounce?


#1

Hello,

My proficiency with JS is basically copy/paste, but I have a snipped I have used a few times before that works fine on Wordpress, but appearently not on JS.

The snippit below normally works to automatically click on an element as soon as the page loads.

<script>
$("document").ready(function(){
$(".selector").trigger("click");
});
  </script>

However it does not appear to work on Unbounce. Does anyone know what the issue might be? I tried it in all three possible locations for a script.


#2

I’ll loop in some of our more code-savvy individuals here to see if they can help you out, Zander :slight_smile:

@Rob @Noah @leah.ann If any of you have a moment, this might be a quick win :tada:


#3

Hey Zander, the reason this code isn’t working is because it is targeting a class that doesn’t exist in Unbounce “.selector”. You’ll need to change that part of the code to reference an element on the page.

You can find the metadata for any given element right in the page builder. Bottom of the properties panel when the element is selected. Here’s a screenshot: https://www.screencast.com/t/ivwmK5Xw7upk

So to get the code working just swap out “.selector” with an ID or class of an Unbounce page element.

Hope this helps!


#4

Hi Rob,

My apologies. The “.selector” is just a placeholder. I was using the ID of the object I was trying to target, and other JS I tested was able to target up the object successfully based on that ID.

I don’t know if it makes a difference, but the object targeted is not actually an Unbounce element, but rather link placed in a custom HTML element.


#5

It should still work even if it’s not an Unbounce element. You could try this:

 <script>
 $("document").ready(function(){
   $(".selector")[0].click();
 });
 </script>

It’s pretty much the same thing. I’ve used the code above before though with Unbounce to trigger a click event.

Feel free to share the URL for the page as well, happy to take a closer look for you. Just let me know which element you are needing to trigger the click on :slight_smile: