Is there a way to click on a link on my page using JavaScript?
document.getElementById('yourLinkID').click();
target="_blank"
property, the browser's popup blocker will be activated for the new window. –
Tabescent If you only want to change the current page address, you can do that by simply doing this in Javascript :
location.href = "http://www.example.com/test";
mailto:
link in userjs script. Definitely saved me time. I was prepared to create a
element and making some "programmatic click" #809557 –
Procne click()
method proposed elsewhere and above and it did not work in IE9, but setting location.href
actually sent the email from the mailto:
link. Great solution! –
Taboo This function works in at least Firefox, and Internet Explorer. It runs any event handlers attached to the link and loads the linked page if the event handlers don't cancel the default action.
function clickLink(link) {
var cancelled = false;
if (document.createEvent) {
var event = document.createEvent("MouseEvents");
event.initMouseEvent("click", true, true, window,
0, 0, 0, 0, 0,
false, false, false, false,
0, null);
cancelled = !link.dispatchEvent(event);
}
else if (link.fireEvent) {
cancelled = !link.fireEvent("onclick");
}
if (!cancelled) {
window.location = link.href;
}
}
target
attribute or <base target="frame">
–
Rountree Simply like that :
<a id="myLink" onclick="alert('link click');">LINK 1</a>
<a id="myLink2" onclick="document.getElementById('myLink').click()">Click link 1</a>
or at page load :
<body onload="document.getElementById('myLink').click()">
...
<a id="myLink" onclick="alert('link click');">LINK 1</a>
...
</body>
The jQuery way to click a link is
$('#LinkID').click();
For mailTo link, you have to write the following code
$('#LinkID')[0].click();
a
link. Using the [0].click()
version works, which I believe is the same as the document.getElementById('yourLinkID').click();
answer. –
Socialization For me, I managed to make it work that way. I deployed the automatic click in 5000 milliseconds and then closed the loop after 1000 milliseconds. Then there was only 1 automatic click.
<script> var myVar = setInterval(function ({document.getElementById("test").click();}, 500)); setInterval(function () {clearInterval(myVar)}, 1000));</script>
Many of the above methods have been deprecated. It is now recommended to use the constructor found here
function clickAnchorTag() {
var event = document.createEvent('MouseEvent');
event = new CustomEvent('click');
var a = document.getElementById('nameOfID');
a.dispatchEvent(event);
}
This will cause the anchor tag to be clicked, but it wont show if pop-up blockers are active so the user will need to allow pop-ups.
Instead of clicking, can you forward to the URL that the click would go to using Javascript?
Maybe you could put something in the body onLoad to go where you want.
You could just redirect them to another page. Actually making it literally click a link and travel to it seems unnessacary, but I don't know the whole story.
for those wanting to click all links that have a particular text content, this would work:
for (const a of document.querySelectorAll("a")) {
if (a.textContent.includes("<your text to be searched here>")) {
a.click();
}
}
reference: https://mcmap.net/q/98838/-how-to-get-element-by-innertext
Client Side JS function to automatically click a link when...
Here is an example where you check the value of a hidden form input, which holds an error passed down from the server.. your client side JS then checks the value of it and populates an error in another location that you specify..in this case a pop-up login modal.
var signUperror = document.getElementById('handleError')
if (signUperror) {
if(signUperror.innerHTML != ""){
var clicker = function(){
document.getElementById('signup').click()
}
clicker()
}
}
You can't make the user's mouse do anything. But you have full control over what happens when an event triggers.
What you can do is do a click on body load. W3Schools has an example here.
© 2022 - 2024 — McMap. All rights reserved.
.click()
on an<a>
element and it worked. – Hutson