0

I want to write a function that will run when the "RUN TEST" button is pressed. This function is designed to hide the two buttons on the next page. Because the application reloads the entire page after pressing this button.

My problem is that when I press the "RUN TEST" button, the buttons I want to hide are not available yet.

$(document).ready(function () {
const button = findButtonByText("RUN TEST");

$(document).on("click", button, function() {
    hideUnnecessaryButtons();
});

function hideUnnecessaryButtons() {
    const buttonsToHide = ["DELETE", "ADD PERSON"];
    for(let j = 0; j < buttonsToHide.length; j++) {
        const button = findButtonByText(buttonsToHide[j]);              
        if(button !== undefined){
            button.style.display = "none";
        }
    }
}

function findButtonByText(text) {
    const buttons = document.querySelectorAll('button');
    for (let i = 0, l = buttons.length; i < l; i++) {
        if (buttons[i].innerText == text){
            return buttons[i];
        }
    }
}
});

How could I improve this feature to access loaded buttons this way?

VLAZ
  • 26,331
  • 9
  • 49
  • 67
John Doe
  • 319
  • 2
  • 5
  • 24
  • 2
    "on the next page" If the page loads a new page there is NOTHING you can do to change the content on the next page. – epascarello Feb 07 '22 at 14:23
  • @epascarello, when i clicked button "RUN TEST" whole page is reloaded. So maybe my function must wait for buttons that i want to hide, because i don't have access to them. – John Doe Feb 07 '22 at 14:30
  • Have you tried this https://stackoverflow.com/questions/16149431/make-function-wait-until-element-exists/47776379? – Ricardo Garcia Landete Feb 07 '22 at 14:32
  • 1
    if your page is relaoded, so all js code disapear too, your question has no logic – Mister Jojo Feb 07 '22 at 14:32
  • 2
    If the page redirects/refreshes after the button is pressed, you might want to try add a parameter to the url query parameters and then to check if it exists after the page gets loaded. – nick zoum Feb 07 '22 at 14:36
  • 1
    When the page navigates away to a new page, any code that was on the previous page is dead. You can not run anything to target the next page. So you would have to code something on the next page to handle it. Typically you would do that with querystrings. The other page looks for the querystring, if it exists, you perform an action. – epascarello Feb 07 '22 at 14:45

0 Answers0