Trying to change the URL based on device for a Squarespace button. It's worth mentioning that the element is created dynamically.
I've gotten searching by class to work as I couldn't get search by id or the query selector to work. The problem now is that search by class generates a list of nodes and I can't seem to get a for loop to correctly select the href.
Might be some other issue I'm not aware of.
This is the code I've tried so far
<div class="sqs-block button-block sqs-block-button" data-block-type="53" id="block-yui_3_17_2_1_1634189249286_8011"><div class="sqs-block-content" id="yui_3_17_2_1_1634205387553_330">
<div class="sqs-block-button-container--center" data-animation-role="button" data-alignment="center" data-button-size="small" id="yui_3_17_2_1_1634205387553_329">
<a href=“http:/google.com” class="sqs-block-button-element--small sqs-block-button-element" data-initialized="true">Learn more</a>
</div>
</div></div>
The class of the first div is the one found by the search
<script>
let n = document.getElementsByClassName("sqs-block button-block sqs-block-button");
for(var i=0; i < n.length; i++)
{
n[i].addEventListener("click", function(e)
{
if(e.target && e.target.nodeName == "a")
{
console.log("item clicked");
}
});
}
</script>