JSFiddle - React, Tailwind, and code Playground

by Dimitar Christoff

HTML

<div data-js-href='#foo'>
    
    <div>will #foo</div>
    <a href="#bar">shouldn't #foo but #bar</a>
    <div id="dynamic"></div>
    
</div>
<div>
    won't foo
</div>

JavaScript

var showURL = function(howLong) {
    // debug.
    return function() {
        console.log(window.location.href);
    }.delay(howLong || 1000);
};

document.body.addEvents({
    "click:relay([data-js-href] a))": function(e) {
        // performance on lookup for repeat clicks.
        var parent = this.retrieve("parent");
        if (!parent) {
            parent = this.getParent("[data-js-href]");
            this.store("parent", parent);
        }
        // communicate it's a dummy event to parent delegator.
        parent.store("linkEvent", e);
        // let it bubble...
    },
    "click:relay([data-js-href])": function(e) {
        // show where we have gone.
        showURL(1500);
        if (this.retrieve("linkEvent")) {
            this.eliminate("linkEvent");
            return;
        }
        var prop = this.get("data-js-href");
        if (prop)
            window.location.href = prop;
        
        
    }
});