Trying to register both value and label from a drowdown

I am desperate now after having tried for two weeks to register both “gender” (Man or Woman) and “genderIcon” (image) from a dropdown element named “gender”. I have added links as values for Man and Woman in the element itself and these links refer to images for respective gender. These values are nicely registered together with all the other stuff when a new user signs up BUT my frustrating problem is that the gender label (Man or Woman) isn’t registered in the “gender” field. Can someone please tell me what I have to add to this code to also register the users gender with text? It shall not be necessary with an extra button.

As you can see I have written “gender” above “genderIcon” but as it is now the current image link also goes to the gender field (shall only show up in the genderIcon field). I have tried with “.label” after $w ( ‘#gender’ ) instead of “.value” but that didn’t work. I also tried with different kinds of queries but that seems hopeless because I get red markings every time and the gender label still refuse to be displayed:

import { currentMember , authentication } from ‘wix-members’ ;
import wixLocation from ‘wix-location’ ;
import wixUsers from ‘wix-users’ ;
import wixData from ‘wix-data’ ;

$w . onReady ( function () {})

function valuesValid ( ) {
return $w ( ‘#email’ ). valid &&
$w ( ‘#password’ ). valid &&
$w ( ‘#forename’ ). valid &&
$w ( ‘#age’ ). valid &&
$w ( ‘#gender’ ). valid &&
$w ( ‘#orientation’ ). valid &&
$w ( ‘#country’ ). valid &&
$w ( ‘#language’ ). valid &&
$w ( ‘#nickname’ ). valid &&
$w ( ‘#state’ ). valid ;
}
function register ( ) {

**if**  ( valuesValid ()) { 
    **let**  email  =  $w ( '#email' ). value 
    **let**  password  =  $w ( '#password' ). value 
    authentication . register ( email ,  password ) 
        . then (( user ) => { 
            **const**  toInsert  = { 
                "_id" :  user . member . _id , 
                "_owner" :  user . member . _id , 
                "loginEmail" :  email , 
                "password" :  password , 
                "firstName" :  $w ( '#forename' ). value , 
                "nickname" :  $w ( '#nickname' ). value , 
                "gender" :  $w ( '#gender' ). value , 
                "genderIcon" :  $w ( '#gender' ). value , 
                "age" :  $w ( '#age' ). value , 
                "sexualOrientation" :  $w ( '#orientation' ). value , 
                "country" :  $w ( '#country' ). value , 
                "state" :  $w ( '#state' ). value , 
                "language" :  $w ( '#language' ). value , 
            }       
            // add the item to the collection 
            wixData . insert ( "Members" ,  toInsert ) 
                . then (( item ) => { 
                    **if**  ( wixUsers . currentUser . loggedIn ) { 
                        wixLocation . to ( `/members/account/ ${ item . _id }`);     
                    }  **else**  { 
                        **let**  email  =  $w ( '#email' ). value ; 
                        **let**  password  =  $w ( '#password' ). value ; 
                        wixUsers . login ( email ,  password ). then (() => { 
                            wixLocation . to ( `/members/account/ ${ item . _id }`); 
                        }). **catch** (( err ) => { 
                            console . error ( err ); 
                            $w ( '#errorMessage' ). text  =  "Couldn't log you in: "  +  err 
                            errorMessage () 
                        }) 
                        console . log ( "User is logged in" ); 
                    } 
                }) 
                . **catch** (( err ) => { 
                    console . error ( err ); 
                    $w ( '#errorMessage' ). text  =  "Something isn't right on your end: "  +  err 
                    errorMessage () 
                }); 
        }) 
}  **else**  { 
    $w ( '#errorMessage' ). text  =  "Something isn't right on your end: There's an Invalid or Missing Property" 
    errorMessage () 
} 

}
function errorMessage ( ) {
$w ( ‘#errorMessage’ ). show ()
$w ( ‘#errorMessage’ ). expand ()
$w ( ‘#regButton’ ). enable ()
$w ( ‘#regButton’ ). label = ‘Register’
setTimeout (() => {
$w ( ‘#errorMessage’ ). hide ( ‘fade’ )
}, 10000 )
setTimeout (() => {
//$w(‘#errorMessage’).collapse() ← Collapse if you want
}, 10500 )
}
export function regButton_click ( event ) {
register ()
}

First of all some basic knowledge for you how to use this FORUM the right way…

  1. Never show CODE as PICS!!! → Anybody wants to waste time to retype your presented CODE from pic back to dev-environment! → WASTE OF TIME !!!

This is why you never got an answer here…

  1. When you show your code → always use the CODE-BLOCK!!!

So one more time your code in an nicely looking and good readable CODE-STRUCTURE …


import { currentMember, authentication } from 'wix-members';
import wixLocation from 'wix-location';
import wixUsers from 'wix-users';
import wixData from 'wix-data';

$w.onReady(function () {})

function valuesValid() {
    return (
        $w('#email').valid &&
        $w('#password').valid &&
        $w('#forename').valid &&
        $w('#age').valid &&
        $w('#gender').valid &&
        $w('#orientation').valid &&
        $w('#country').valid &&
        $w('#language').valid &&
        $w('#nickname').valid &&
        $w('#state').valid
    );
}
function register() {
    if (valuesValid()) {
        let email = $w('#email').value
        let password = $w('#password').value
        authentication.register(email, password)
        .then((user) => {
            const toInsert = {
                "_id": user.member._id,
                "_owner": user.member._id,
                "loginEmail": email,
                "password": password,
                "firstName": $w('#forename').value,
                "nickname": $w('#nickname').value,
                "gender": $w('#gender').value,
                "genderIcon": $w('#gender').value,
                "age": $w('#age').value,
                "sexualOrientation": $w('#orientation').value,
                "country": $w('#country').value,
                "state": $w('#state').value,
                "language": $w('#language').value,
            }      
            // add the item to the collection
            wixData.insert("Members", toInsert)
            .then((item) => {
                if (wixUsers.currentUser.loggedIn) {
                    wixLocation.to(`/members/account/${item._id}`);    
                } else {
                    let email = $w('#email').value;
                    let password = $w('#password').value;
                    wixUsers.login(email, password).then(() => {
                        wixLocation.to(`/members/account/${item._id}`);
                    }).catch((err) => {
                        console.error(err);
                        $w('#errorMessage').text = "Couldn't log you in: " + err
                        errorMessage()
                    })
                    console.log("User is logged in");
                }
            })
            .catch((err) => {
                console.error(err);
                $w('#errorMessage').text = "Something isn't right on your end: " + err
                errorMessage()
            });
        })
    } else {
        $w('#errorMessage').text = "Something isn't right on your end: There's an Invalid or Missing Property"
        errorMessage()
    }
}
function errorMessage() {
    $w('#errorMessage').show()
    $w('#errorMessage').expand()
    $w('#regButton').enable()
    $w('#regButton').label = 'Register'
    setTimeout(() => {
        $w('#errorMessage').hide('fade')
    }, 10000)
    setTimeout(() => {
        //$w('#errorMessage').collapse() <- Collapse if you want
    }, 10500)
}
export function regButton_click(event) {
    register()
}

Next lesson!!!
Anybody knows which ELEMENT is/are…

"gender": $w('#gender').value,
"genderIcon": $w('#gender').value,

This for read the followig post…

There you will find hints how to define your elements-IDs better in future, so that also other EXTERN-PROGRAMMERS who do not know your project-setup can easely read your code and understand it. —> AND AGAIN!!! → ANOTHER SITUATION, WHERE YOU AND THE EXTERN-CODER looses time for nothing!!!

Structure your code the most efficient way (not just for you!).

Back to your problem: You gave us the hint that these elemets are DropDowns, right?

"gender": $w('#gender').value,
"genderIcon": $w('#gender').value,

So let’s first change the Element-ID to the correct ID-naming…
…using “dd” or “ddn” as shortcut for your DropDown-element, to identify it later inside your code…
I would suggest you always use a TRIPPLE-LETTER, like shown in the other post!!!

"gender": $w('#ddnGender').value,
"genderIcon": $w('#ddnGender').value,

So how is structured a DropDown (ddn) ???

A DropDown has OPTIONS consisting of…
a) —> label
b) —> value

EXAMPLE: Setting data into a DropDown…

$w("#myDropdown").options = [
  {"label": "Who's on first!", "value": "first"},
  {"label": "What's on second", "value": "second"},
  {"label": "I Don't Know is on third", "value": "third"}
];

Now this DropDown has Data-Options in it.

Now let us get the OPTIONS back out of the DropDown…
let ddnOptions = $w(“#myDropdown”).options;
…we console-log it…

console.log("DDN-OPTIONS: ", ddnOptions);

When you take a look onto your results in the CONSOLE, you will see this one…

[
  {"label": "Who's on first!", "value": "first"},
  {"label": "What's on second", "value": "second"},
  {"label": "I Don't Know is on third", "value": "third"}
];

Now again back to your issue → What is wrong in your CODE ?

As you can see, we have here an → ARRAY ← holding some → OBJECTS ← in it.

That means, if you want to get some data out of a DropDown, you first have to tell where to find your VALUE or LABEL!!!

let myWishedLabel = ddnOptions[0].label
let myWishedValue = ddnOptions[0].value

And always try to take a look onto the VELO-API-DOCS!

Thank you Ninja for your pedagogic lesson and I will try to remember what you wrote :slightly_smiling_face: BUT it is still hard to know exactly where I shall place the code inside the total code for this registration page. The hardest part for me is always when an expert or experienced guy already have made a great set up and I then want to add an extra function. It often fail because of red cursors that tell me that I cant place it here or there. This screenshot shows a good example of the difficulties to add a new code snippet into a bigger one.

If I place it before this code it doesn´t work and if I place it afterwards it doesn´t work and I don´t have a clue of where inside the code I shall place the bastard??

By the way, “gender” and “genderIcon” that you have marked yellow are not dropdowns but collection fields. But the dropdown is also named to “gender” which is unecessary confusing so I´m gonna rename it to gdd (gender dropdown).

Ok, one more little lesson.

Always try to reply to the correct post-reply.
The post looses OVERVIEW, if we do not answer to the last comment.
Keep this also on your mind.

Ok, i would say → send me an invitation to your project, i will take a look onto it, if you want.

My mail can be found in my profile.

Yes I know, I was tired and didn´t think properly :wink: I have now sent you an invitation with the role as website manager.

Kristofer

Ok, i think i got your problem!

This will help you out!

$w.onReady(function() {
    console.log("DropDown-Options: ", $w('#ddnGender').options);

    $w('#ddnGender').onChange((event)=>{
        console.log("Selected-DropDown-Index: ", $w('#ddnGender').selectedIndex); 
        console.log("Gender chosen: "+$w('#ddnGender').options[$w('#ddnGender').selectedIndex].label);
    });
    $w('#ddnGender').onChange(()=>{console.log("Gender-Icon chosen: "+$w('#ddnGender').value);});
    $w('#btnSubmit').onClick(()=>{console.log("Registration-process started..."); register();});
});

Take a look onto the given CONSOLE-LOGS in your CONSOLE.
I did some changes on your page, just take a look onto the logs, they will show you the right way.

Now you are able to get the - - → VLAUE ← - - and the - - ->LABEL<—!!!

Also changed code-part…

"gender": $w('#ddnGender').options[$w('#ddnGender').selectedIndex].label,
"genderIcon": $w('#ddnGender').value,

Test it!

Thanks but I made two registrations after your changes but got this result both times. As you can se in this screenshot, below Gender it shall display “Man” and not the link to the male icon image. You can see the same result in the collection. The reason to why earlier registrations showed male or female was that I didn´t register gender icons.

That was not working because you did not have any connection between your actions.

  1. You choose something from dropdown
  2. You click the submit-button

These are 2 different actions, but there was no cummunication between these two sub-steps (no data-transfer).

You have first to save the choosen values inside a variable and get the values of the memorized variable when you click onto submit.

This for you can simply create GLOBAL-VARIABLES…

import { authentication } from 'wix-members';
import wixLocation from 'wix-location';
import wixUsers from 'wix-users';
import wixData from 'wix-data';

let genderValue;
let genderLabel;

$w.onReady(function() {console.log("Page is ready --> CODE-START!!!");
    console.log("DropDown-Options: ", $w('#ddnGender').options);

    $w('#ddnGender').onChange((event)=>{
        //console.log("Selected-DropDown-Index: ", $w('#ddnGender').selectedIndex); 
       // console.log("Gender chosen: "+$w('#ddnGender').options[$w('#ddnGender').selectedIndex].label);

       genderValue = $w('#ddnGender').value; console.log("VALUE: ", genderValue);
       genderLabel = $w('#ddnGender').options[$w('#ddnGender').selectedIndex].label; console.log("Label: ", genderLabel);
    });
    $w('#ddnGender').onChange(()=>{console.log("Gender-Icon chosen: "+$w('#ddnGender').value);});
    $w('#btnSubmit').onClick(()=>{register();});
});
$w('#btnSubmit').onClick(()=>{register();});
function register() {console.log("Registration-process started...");
    if (valuesValid()) {console.log("All values = VALID!!!");
        let email = $w('#email').value
        let password = $w('#password').value
        authentication.register(email, password)
            .then((user) => {
                const toInsert = {
                    "_id": user.member._id,
                    "_owner": user.member._id,
                    "loginEmail": email,
                    "password": password,
                    "firstName": $w('#forename').value,
                    "nickname": $w('#nickname').value,
                    "age": $w('#age').value,
                    "gender": genderLabel,
                    "genderIcon": genderValue,
                    .....
                    ....
                    ...
                    ..
                    .

As you can see, the last registered member on your site, got the Gender-Icon working and also the gender itself works now.

I didn´t mean to give a lazy or unwilling impression but just thought that your intention was to make everything work. You know, a teacher can teach at the same time he creates something until it´s finished :blush: I will copy and save all your instructions for future use and I am very thankful for your help.