• 0

[Javascript] Add onclick event to button which has random ID


Question

Hi all,

Got a website on squarespace which unfortunately is somewhat limited when it comes to adding code to buttons. An onlick would be ideal and I can add it via javascript HOWEVER the damn div ID changes each time the page is generated.

I need to somehow use js to search the page, find the button which has particular text in it and then I can use document.getElementById("ETC").onclick to perform what I need.

My head hurts just thinking about this.

Any clues?

3 answers to this question

Recommended Posts

  • 0

If you can use jquery and want to select based on the content: https://api.jquery.com/contains-selector/

Also if the id value is something like "yolo-button-546366" you could use the following selector: "button[id^=yolo-button-]"

using [ ] to select elements with properties in combination with ^=, ~=, *=, $= etc is very powerful.

  • 0

Look for any other optional indicators you can use.

  • Can you apply a unique class?
  • Is it contained in a unique element & can you apply any classes or indicators do that?
  • Can you apply a data attribute to the button?

If you can apply a data attribute to the button, then that's personally the option I would recommend. You can then initiate this using jQuery relatively simply as noted above.

  • 0

I like to use a unique class as mentioned above.  It doesn't even need to be a real class.

Add a unique class name like this to your button:

<input id="mybutton" type="button" value="click me" class="_myuniqueclassname" />

 

Add the jquery click event like so:

$("input._myuniqueclassname").on("click", function() {
    //do something here
});
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Popular Now

  • Posts

    • how am i gonna teach my grandma understand a new OS now ?
    • Well, Satya Nadella did say 30% of Microsoft's code is written by AI. Considering that code is also reviewed and tested by the same AI: garbage in, Microsoft code out.
    • Spotify 1.2.66.445 by Razvan Serea Spotify for Windows is your ultimate music and podcast streaming app, designed for a seamless listening experience. Enjoy millions of songs, curated playlists, and personalized recommendations tailored to your taste. Spotify delivers high-quality audio streaming with bitrates up to 320 kbps for Premium users and 160 kbps on the Free tier. Music is streamed in Ogg Vorbis format on the app and AAC on web browsers, ensuring efficient delivery and compatibility. Adaptive bitrate technology adjusts quality based on network conditions, while Premium users enjoy offline downloads for uninterrupted playback. Podcasts are streamed at slightly lower bitrates, typically 96-128 kbps, to balance quality and data usage. Spotify works across smartphones, desktops, smart speakers, game consoles, and more, with Spotify Connect enabling smooth cross-device playback. Features like real-time lyrics, curated playlists, and personalized EQ settings elevate the listening experience, making Spotify a versatile platform for music and podcasts alike. Spotify Free features: Stream Millions of Songs - Access a vast library of music across various genres and artists. Curated Playlists - Discover ready-made playlists for every mood and moment. Shuffle Play Mode - Enjoy songs in random order with shuffle-enabled playback. Personalized Recommendations - Get music suggestions based on your listening habits. Access to Podcasts - Explore a wide range of podcasts on topics you love. Ad-Supported Listening - Stream music with occasional audio and display ads. No Subscription Required - Enjoy music without any payment commitment. Cross-Device Sync - Listen seamlessly on mobile, desktop, or web browser. Basic Playback Controls - Play, pause, and skip tracks within shuffle limitations. Discover Weekly & Release Radar - Stay updated with custom playlists for new music. Download: Spotify 1.2.66.445 | ARM64 | ~100.0 MB (Free, paid upgrade available) View: Spotify Website | Web Player Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Understandable, they need to make money somehow to keep the service "free". If you're not paying with money, you'll pay with something else. In this case, it's ads. People will bitch about this (as usual), but it's basic economics, everything has a price, like YouTube (non-premium).
    • A sign of a man with absolutely no taste. Also the first American president to whine about American "jobs" while selling Chinese stuff for 5x or 10x the actual worth while slapping his stupid name on it. Like, what even is this, some really bad, humorless parody? Nope, it's the actual reality. Selling Chinese bible, selling EV's on White House front yard, selling Chinese phones that are wrapped in few microns of gold for 3x the value... It's so hilarious it's cringy af.
  • Recent Achievements

    • Week One Done
      patrickft456 earned a badge
      Week One Done
    • One Month Later
      patrickft456 earned a badge
      One Month Later
    • One Month Later
      Jdoe25 earned a badge
      One Month Later
    • Explorer
      Legend20 went up a rank
      Explorer
    • One Month Later
      jezzzy earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      637
    2. 2
      ATLien_0
      278
    3. 3
      +FloatingFatMan
      171
    4. 4
      Michael Scrip
      156
    5. 5
      Steven P.
      128
  • Tell a friend

    Love Neowin? Tell a friend!