How to assign an e target value in a JS file when the event is taking place from the Lightning Radio button in HTML?

563    Asked by BuffyHeaton in Salesforce , Asked on May 4, 2023

I am writing a code in JS to receive name and value attributes from the Lightning-Input Radio button where an event will take place on selection. But in the JS file, it's giving me an error while assigning the event values to const variables. Please help.

HTML CODE



JS CODE
    changeHandler(event)
    {   
         const {name, value} = event.target;
         const name = event.target.name;
         const value1 = event.target.value;
         console.log('name', event.target.name);
         console.log('value', event.target.value);
        
    }

ERROR LINES Note: Just added both ways of assigning values for clarification.

         const {name, value} = event.target;
         const name = event.target.name;
         const value1 = event.target.value;
ERROR Screenshot

Answered by David EDWARDS

To assign an e target value in JS file when the event is taking place from Lightning Radio button in HTML -

The first issue is not an error, but a warning. You've declared variables (name, value) but never use them in your changeHandler() function.

The second issue should be obvious. In your destructuring, you've already created a name variable. You can't re-use that variable name in const name = event.target.name;

The destructuring (i.e. const {name, value} = event.target;) means that you shouldn't need to declare any more variables (or use event.target again).

You should only need the following in your handler function's body

{
    const {name, value} = event.target;
    console.log('name', name);
    console.log('value', value);
}


Your Answer

Interviews

Parent Categories