Field Input from Dropdown

Hi All, I have a database made up of Type of Exercise and corresponding values. I’m trying to write code so that when you select the name from a dropdown and hit a button the corresponding value appears in a text box which is linked to the database. This is the code I have:

import wixData from 'wix-data'
    
$w.onReady(function () {
    $w("#button4").onClick(() => {
        $w("#dataset1").setFilter(wixData.filter()
        .eq("typeOfExercise", $w("#activity").value));
        });     
    });   

It’s not working but I’m not sure what I’m missing?

Why not doing it the direct way?

import wixData from 'wix-data'

var DATASET = "#dataset1";
var DATAFIELD = "typeOfExercise";
var DROPDOWN = "#dropdown1";

    
$w.onReady(function() {
    $w(DROPDOWN).onChange(()=> {
        let selectedIndex = $w(DROPDOWN).selectedIndex; console.log(selectedIndex);
        let selectedValue = $w(DROPDOWN).value; console.log(selectedValue);
        let VALUE = selectedValue;
        
        $w(DATASET).setFilter( wixData.filter()
        .eq(DATAFIELD, VALUE))
        .then(() => {console.log("Dataset is now filtered");})
        .catch((err)=> {console.log(err);});
    });     
});  

When I put this code in I get the error $w(…)onChange is not a function, do you know why this is? Sorry if it’s a foolish question, I’m quite new to coding

@andrewjlhawkins
Did you updated the ELEMENT-IDs ?
Your DROPDOWNS-ID is → “#dropdown1” ???

Do the lines starting var not do that in code?

If it does not work …
Place it this way…

$w.onReady(function() {
    let DATASET = "#dataset1";
    let DATAFIELD = "typeOfExercise";
    let DROPDOWN = "#dropdown1";
    $w(DROPDOWN).onChange(()=> {.......

I think i forgot something…

import wixData from 'wix-data'
  
$w.onReady(function() {
    let DATASET = "#dataset1";
    let DATAFIELD = "typeOfExercise";
    let DROPDOWN = "#dropdown1";
    
    $w(DATASET).onReady(()=>{
        $w(DROPDOWN).onChange(()=> {
           let selectedIndex = $w(DROPDOWN).selectedIndex; console.log(selectedIndex);
           let selectedValue = $w(DROPDOWN).value; console.log(selectedValue);
           let VALUE = selectedValue;
           
           $w(DATASET).setFilter( wixData.filter()
           .eq(DATAFIELD, VALUE))
           .then(() => {console.log("Dataset is now filtered");})
           .catch((err)=> {console.log(err);});
        });  
    });   
}); 

@russian-dima Thank you so much, I figured out how to make it work! You’re a life saver!

@andrewjlhawkins
No problem.