Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7399459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:03:48+00:00 2026-05-29T04:03:48+00:00

In the following code I want to reduce these 5 functions down to 3.

  • 0

In the following code I want to reduce these 5 functions down to 3.

The first function toggle_visibility() is already made universal by passing the id when I call the function from my html, however, I have to repeat the next two functions thankYouText_Change() and resettxt() because I don’t know how to store the value of the Item variable, nor the p or OK_button variables and pass them to the next function so that they can be used by the other functions.

My goal is to figure out how to reduce these to a set of 3 functions that can be accessed at anytime in my html and applied to any and all relevant elements simply by using onClick="function_foo('desired_element_foo'), without having to have a separate set of functions for each time I want to use them on a different element.

I think that in order to do this I also need to know how to make the variables p and OK_Button have values that will automatically change and be stored based upon the id that I send to them/access them with.

function toggle_visibility(id) {
    var Item = document.getElementById(id);

    if (Item.style.display == 'block') {
        Item.style.display = 'none';
    }
    else {
        Item.style.display = 'block';
    }
}
function thankYouText_Change() {
    var p = document.getElementById("thanksForEmail");
    var OK_Button = document.getElementById("okButton");

    if (p.innerHTML == 'Thank you for submitting your e-mail.') {
        OK_Button.style.display = 'none';
        p.innerHTML = "Returning to page...";
        setTimeout("toggle_visibility('msgSend'), resettxt()", 500);
    }
}
function resettxt() {
    var p = document.getElementById("thanksForEmail");
    var OK_Button = document.getElementById("okButton");

    if (p.innerHTML == 'Returning to page...') {
        p.innerHTML = 'Thank you for submitting your e-mail.';
        OK_Button.style.display = 'block';
    }
}
//Start of repeated functions for second div and button elements
function thankYouText_Change2() {
    var p = document.getElementById("thanksForEmail2");
    var OK_Button = document.getElementById("okButton2");

    if (p.innerHTML == 'Thank you for submitting your e-mail.') {
        OK_Button.style.display = 'none';
        p.innerHTML = "Returning to page...";
        setTimeout("toggle_visibility('msgSend2'), resettxt2()", 500);
    }
}
function resettxt2() {
    var p = document.getElementById("thanksForEmail2");
    var OK_Button = document.getElementById("okButton2");

    if (p.innerHTML == 'Returning to page...') {
        p.innerHTML = 'Thank you for submitting your e-mail.';
        OK_Button.style.display = 'block';
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T04:03:49+00:00Added an answer on May 29, 2026 at 4:03 am

    For a first pass, you could simplify this to something like this:

    function thankYouText_Change(pId, okId, msgSendId){
        var p = document.getElementById(pId);
        var OK_Button = document.getElementById(okId);
    
        if(p.innerHTML == 'Thank you for submitting your e-mail.'){
            OK_Button.style.display = 'none';
            p.innerHTML = "Returning to page...";
            setTimeout(function(){
              toggle_visibility(msgSendId);
                resettxt(pId, okId);
            }, 500);
        }
    }
    
    function resettxt(pId, okId){
        var p = document.getElementById(pId);
        var OK_Button = document.getElementById(okId);
    
        if(p.innerHTML == 'Returning to page...'){
        p.innerHTML = 'Thank you for submitting your e-mail.';
        OK_Button.style.display = 'block';}
    }
    

    And then for each set of elements on the page, you just need to call thankYouText_Change with the correct IDs for each of the 3 related elements.

    For a 2nd pass, you could simplify both of my above functions into one, so that you don’t need to re-call document.getElementById on the same elements more than once (not significant, but I also like to declare everything with var – variables and functions alike):

    var thankYouText_Change = function(pId, okId, msgSendId){
        var p = document.getElementById(pId);
        var OK_Button = document.getElementById(okId);
    
        if(p.innerHTML == 'Thank you for submitting your e-mail.'){
            OK_Button.style.display = 'none';
            p.innerHTML = "Returning to page...";
            setTimeout(function(){
              toggle_visibility(msgSendId);
                if(p.innerHTML == 'Returning to page...'){
                    p.innerHTML = 'Thank you for submitting your e-mail.';
                    OK_Button.style.display = 'block';
                }
            }, 500);
        }
    }
    

    (Note that this eliminates the need for a resettxt function.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the following code I want to replace every occurrence of U.S.A with united
I have the following code I want to run, but the problem is $this->type
The following code does not want to compile. See the included error message. Code:
In following code, I want to extend the behaviour of a class by deriving/subclassing
The following code will generate a link to the page I want to get
I want to run the following code: ajaxUpdate(10); With a delay of 1 second
I want to use the following code to login to a website which returns
I want to execute python code from C# with following code. static void Main(string[]
I have the following code: [ [NSDate date] descriptionWithLocale: @yyyy-MM-dd ] I want it
I have written the following code choice /m Do you want to add another

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.