0
I need to cancel the anchor action after I send an alert. How do I do this?
I tried with e.eventDefault()
. I tried with the window.location
to redirect the page to the default and I’m not getting the result I want.
<script type="text/javascript" defer>
function alertPages() {
alert("Página em contrução")
const a = document.getElementsByTagName('a')
a.preventDefault()
}
</script>
<a href="./about.html" onclick="alertPages()">About</a>
<a href="./contact.html" onclick="alertPages()">Contato</a>
In other words, when clicking on the link, show the message not redirect to the page.
shouldn’t be:
preventDefault()
?– Danizavtz
tested with the
preventDefault()
and it didn’t work?– Danizavtz
@Danizavtz of course not,
preventDefault
is for events, so he would have to add a eventListener for the taga
. Detail thatgetElementsByTagName
returns a Htmlcollection.– Cmte Cardeal
but I added there, because I’ve just tried with Event Listener too and it wasn’t.
– Uelber Henrique
getElementsByTagName
returns a Htmlcollection, Voce would have to convert and iterate to add a Event Listener.– Cmte Cardeal
@Cmtecardeal I am unable to debug, so I asked. sry.
– Danizavtz