How to store the selected value in a variable?

rick ray

The user can choose their gender. I need to somehow write the selected response to a variable so that I can use it later when submitting the form. How can i do this? The choice itself is implemented using pop-up buttons when passing the questionnaire:

function genderNext() {
  $(".chooseGenderM").click(() => {
    document.querySelector(".chat-content-buttons-gender").style.display = "none";
    myMassange("Male");

    setTimeout(() => {
      process = true;
    }, 500);
    scrollDown();
  });
  $(".chooseGenderW").click(() => {
    document.querySelector(".chat-content-buttons-gender").style.display = "none";
    myMassange("Woman");

    setTimeout(() => {
      process = true;
    }, 500);
    scrollDown();
  });
}

jsejcksn

You can store it in a property on an object variable (or directly in a mutable variable using let) elsewhere in your program, and then reference the value when you need to submit the form:

// Elsewhere, in a higher scope in your program
// that you can access "later when submitting the form":

const userData = {
  gender: "Unknown", // or initialize it as undefined, etc.
};

// In the function you showed, assign the gender to the associated property:
function genderNext () {
  $(".chooseGenderM").click(() => {
    document.querySelector(".chat-content-buttons-gender").style.display = "none";
    // Here:
    userData.gender = "Male";
    myMassange("Male");

    setTimeout(() => {
      process = true;
    }, 500);
    scrollDown();
  });
  $(".chooseGenderW").click(() => {
    document.querySelector(".chat-content-buttons-gender").style.display = "none";
    // And here:
    userData.gender = "Woman";
    myMassange("Woman");

    setTimeout(() => {
      process = true;
    }, 500);
    scrollDown();
  });
}

// "later when submitting the form", use the value however you want to. For example:
submitForm(userData.gender);

Alternatively, if you can't create or assign to a higher-scoped variable for some reason, then you can assign the string value to a property on an element's .dataset property and retrieve it later. See Using data attributes for more info.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get the value of a selected option dynamically and store it into a variable?

How to store the user selected value from API response in a variable

How to store in a JavaScript variable the selected rows in a table

How to make a simple number picker in Xamarin Forms and store selected value in a variable

How validate if a specific value was selected and store it in Laravel

How to store the ID of selected dropdown value in SQLAlchemy

How to store a deleted value in a variable?

How to store a mysql value into a variable?

How to assign the value of selected date to a variable

How to store in a variable, the range address from a user selected range

How to store return value of a PLSQL function into a variable

How to store a return value in variable flutter

how to store a select statement return value in a variable

How to store redis.get value to a variable

How to store value from grep output in variable

How to store PHP defined constant (value) as a variable?

How to store a C++ variable as a value in NSDictionary?

How to store value of a filed into a variable in Solarium?

How to store the return value of suspended function to a variable?

How to store a value from firebase in a global variable?

How to store a value in a variable in a callback function in vuejs

How to add rem to pixel value and store in variable?

How to store in Angular fuction return value into variable

How to store value from datepicker into string variable

Powershell and SQL: How to store the value of a query in a variable?

How to store the value of a select in a variable in laravel

how to store tuple value as python variable

How to store value of function call to a variable

How to store the resolved value of a promise inside a variable?