javascript - listening for click event for an href by classname -
there page basic html cannot touch looks this:
<a class="continue-shopping" href="https://someurl">continue shopping</a>
what want send user different link when click on someurl text link. user can come page containing html many other pages.
i have tried many hours cannot js recognize click event class associated hyperlinked text. use here. js code wrote not work
window.onload = function() { prepeventhandler();
}
function prepeventhandler () { var myclass = document.getelementsbyclassname("continue-shopping"); myclass[0].onclick=window.open(document.referrer,"_self"); /* make pages go haywire or -- not work */ myclass[0].addeventlistener("click", function() { window.open(document.referrer,"_self"); } ) }
it keeps ignoring second function, , sure doing basic wrong. again, help!
apart preventdefault()
use return false
window.onload = function () { var myclass = document.queryselector(".continue-shopping") .onclick = function () { window.location.href = "http://elsewere.com"; return false; } }
Comments
Post a Comment