Ebower
Ebower

Reputation: 33

How to make a button click a button on a different page

I have a button on one html page, but when clicked I want it to activate a different button on a different html page. Is this possible?

Upvotes: 0

Views: 622

Answers (2)

Mouser
Mouser

Reputation: 13304

This solution will work though, but it requires passing a value from one page to another. It cannot link pages together. If you want that look into websockets.

Page 1

HTML

<button id="activate">Activate other page</button>

JavaScript

document.querySelector("#activate").addEventListener("click", function(){
    location.href = "page2.html?button=activate"; //mind the querystring here.
});

Page 2

HTML

<button id="toactivate" disabled>disabled button</button> 

JavaScript

var activateButton = location.search.slice(1); //querystring | get rid of the ?
activateButton.split(/&/);
activateButton.forEach(function(value){
    var splitted = value.split("=");
    if (splitted[0] == "button" && splitted[1] == "activate")
    {
       document.querySelector("#toactivate").removeAttribute("disabled");
    } 
});

Upvotes: 0

Scott Marcus
Scott Marcus

Reputation: 65806

This is only possible if the first page actually generates the second page (via window.open()). Otherwise, you won't have any way to access the document in the other window.

Here's an example:

var newWin = window.open("other page in my domain");
var otherButton = newWin.document.querySelector("selector to find button");
otherButton.click();

Upvotes: 1

Related Questions