Modifying Button on a Form using Javascript

Hi Everyone!

I’m new to Javascript and would greatly appreciate anyone’s help. I’m currently working on a Form project. I’m creating an anonymous function to modify and display the buttons correctly once a user’s multiple choice results are displayed.

The button text should display: CV Submitted; the background color of the button should be grey
and text color should be white - all done through Javascript.
Once the submit button is clicked, the Anonymous function executes.

Note:

  1. The “submit” button is hidden by default.
    In the CSS document, its displayed as:
#submit {
  display: none;
}

I need to render it visible when my anonymous function is executed.

  1. Here is my Anonymous Function:
var submitBtn = document.getElementById('submit');

submitBtn.addEventListener("submit", function (e) {
  e.preventDefault();
  document.getElementById("submit").style.visibility = "visible";
  submitBtn.innerHTML = "<button id='submit' style = color:white; background:grey>CV Submitted</button>";
});

a. Note that this button does not have to have data submission functionality.

b. The “button” button should acquire reset functionality once it has been clicked the first time. To achieve this, execute the below method in an appropriate function:

window.location.reload(true);

Well, lets address something in your stated requirements first of all:

How does one click on something that is hidden? Are you sure you got the flow the right way around? Are you supposed to hide the button when its clicked?

Hello m_hutley!

Thank you so much for you response.

Correct,

  1. The “submit” button is hidden by default. I have to use Javascript to render it visible when this function is executed.

  2. Once the “submit” button is clicked, I need to execute the following by calling an anonymous function:

  3. The text on the button should read “CV Submitted”

  4. The background of the button should be styled grey.

  5. The text color on the button should be white.

  6. Note that this button does not have to have data submission functionality in the scope of this assignment.

  7. The “button” button should acquire reset functionality once it has been clicked the first time. To achieve this, execute the below method in an appropriate function:

window.location.reload(true);

Okay. Maybe I can restate this to help you see why that doesnt make sense.

“I am hiding an apple. When you touch the apple, I will show you where the apple is.”

If… if i can touch the apple, I can already see it. If i cant see it, i cant touch it…

1 Like

You can check out my code here - https://github.com/Strider64/phototechguru/blob/master/assets/js/game.js

and to see a working copy of it.

https://phototechguru.com/game.php

I think it has some of the things you are looking for? :thinking:

Hi @Loranne,

To format the code in your posts select your code and click on the </> button in the menu at the top of the text editor. It will make it easier for people to read and give you assistance. I have done it for you this time :slight_smile:

Just a few observations:

Hidden button

  1. The “submit” button is hidden by default. I have to use Javascript to render it visible when this function is executed.

As m_hutley has pointed out, you can’t click on something if it isn’t there. The function will never be executed.

Changing visibility

document.getElementById(“submit”).style.visibility = “visible”;

It might be better to use a class with a name like ‘hidden’ instead.

CSS

.hidden {
  display: none;
}

HTML

<!-- has class of hidden, so not displayed -->
<div id='myElement' class='hidden'>...</div>

JS

const myElement = document.querySelector('#myElement');
// to show myElement remove that 'hidden' class
myElement.classList.remove('hidden');

For more on classList

Resetting the form

Instead of reloading the page you could look at using the form’s reset method.

From his first post I think there is a selection element or other input element.
Once the selection or input has happened the hidden submit button is revealed and then runs his annon. function.
Without seeing the html and js code erroneous assumptions maybe being made.
And if he is just learning why is he learning ancient practices instead of current best practices?

1 Like

Hello Dennisjn,

Thank you so much for looking into this. Here is the entire HTML. Please let me know how I can fix this button issue based on his criteria.

<div id="assessment">
      <h3>Section 1: Education</h3>

      <div id="education1">
        <p>What is your highest level of education?</p>

        <select id="education-level">
          <option id="postgraduate" value="postgraduate">Postgraduate</option>
          <option id="graduate" value="graduate">Graduate</option>
          <option id="secondary" value="secondary">Secondary</option>
        </select>

        <p>
          Was Mathematics one of your majors at your highest level of education?
        </p>

        <select id="mathmajor">
          <option id="mathmajor-yes" value="Had math as a major">Yes</option>
          <option id="mathmajor-no" value="Did not have math as a major">
            No
          </option>
        </select>
      </div>

      <h3>Section 2: Work History</h3>

      <div id="work-history1">
        <p>
          With which of the following teaching areas do you have most
          experience?
        </p>

        <select id="areas">
          <option id="teaching-primary" value="Primary education">
            Primary education
          </option>
          <option id="teaching-secondary" value="Secondary education">
            Secondary education
          </option>
          <option id="teaching-tertiary" value="Tertiary education">
            Tertiary education
          </option>
          <option id="teaching-none" value="no audiences">
            I have no teaching experience
          </option>
        </select>

        <p>How many years' worth of teaching experience do you have?</p>

        <select id="years">
          <option id="teaching-one" value="0 to 2 years">0 to 2 years</option>
          <option id="teaching-three" value="3 to 6 years">3 to 6 years</option>
          <option id="teaching-seven" value="7 or more years">
            7 or more years
          </option>
        </select>
      </div>

      <button id="calculate">Display my CV</button>
      <button id="submit">Submit for evaluation</button>
    </div>type or paste code here

Good Morning m_hutley,

Thank you for your help! I looked at the form and the BUTTON is GREYED OUT.
He wants the button to be visible AFTER the client enters all the data.

Therefore, I will use the attribute: button.disable = false to enable the button.

If I have any problem, I will write again.

Thank you so very much,
Loranne

1 Like

The way I would do this is to add a “change” event handler to each of the three “select” elements.
In those event handlers I would check the “value” of the other two select elements. If they both have a value I would then enable the button.
Read https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement for all the info on select elements.

With the submit button, you should have the JavaScript hide that button when the page is starting to load.

Hide the submit button

Here’s the existing calculate and submit buttons:

        <button id="calculate">Display my CV</button>
        <button id="submit">Submit for evaluation</button>

The submit button needs to be hidden when the page first loads:

so let’s add some CSS to let us hide things:

.hide {
  display: none;
}

and some JavaScript to hide the button:

const submitButton = document.querySelector("#submit");
submitButton.classList.add("hide");

Show the submit button

After that Display my CV button has been clicked, that’s when the Submit button can then be shown.

const calculateCVButton = document.querySelector("#calculate");
...
function calculateCVHandler(evt) {
    calculateCV();
    submitButton.classList.add("hide");
}
...
calculateCVButton.addEventListener("click", calculateCVHandler);

Here’s what that all looks like together. We have three parts:

  1. References to on-page elements
  2. Functions and event handlers
  3. Things that happen when the page initializes
// Variables
const calculateCVButton = document.querySelector("#calculate");
const submitButton = document.querySelector("#submit");

// Functions
function calculateCV(calculateButton) {
    // yet to be implemented
}
// Event Handlers
function calculateCVHandler(evt) {
    calculateCV(event.target);
    submitButton.classList.add("hide");
}

// Init
calculateCVButton.addEventListener("click", calculateCVHandler);
submitButton.classList.add("hide");

Clicking the Display button, lets certain things be checked in the calculateCV function, and results in the submit button being shown.

Use the submit button

Lastly, something wants to be done when you press the submit button.

function submitCV(submitButton) {
    // yet to be implemented
}
...
function submitCVHandler(event) {
    submitCVHandler(event.target);
}
...
submitButton.addEventListener("click", submitCVHandler);

Here’s what that looks like in total:

// Variables
const calculateCVButton = document.querySelector("#calculate");
const submitButton = document.querySelector("#submit");

// Functions
function calculateCV(calculateButton) {
    // yet to be implemented
}
function submitCV(submitButton) {
    // yet to be implemented
}

// Event Handlers
function calculateCVHandler(event) {
    calculateCV(event.target);
    submitButton.classList.remove("hide");
}
function submitCVHandler(event) {
    submitCVHandler(event.target);
}

// Init
calculateCVButton.addEventListener("click", calculateCVHandler);
submitButton.addEventListener("click", calculateCVHandler);
submitButton.classList.add("hide");

That’s the main structure of the calculate and submit code, and you now have a good place to carry on working with things in those functions that are yet to be implemented.

1 Like

WOW! Impressive, Mr. Wilkins! Thank You for putting all this together!

I will certainly incorporate this into my form. I appreciate everyone’s input and I love this site.
It’s really helping me to grow in my new career path.

Much appreciated.
Loranne.

M-hutley,

Just to clarify and I’m sorry I didn’t before.

There are TWO buttons in the HTML. (Submit and Display My CV)
It’s the submit button that was hidden in CSS and has to be rendered visible once the client enters all data.

Display my CV
Submit for evaluation

Hello Awesome Web Developers!!

I’m so GRATEFUL to all of you who helped me with this project. I successfully applied your instructions and IT WORKED!! I COMPLETED MY PROJECT. I LOVE this Website. I ASPIRE to become a Great Developer like the members on this TEAM!

Have a Wonderful Evening! :smiling_face_with_three_hearts:

3 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.