0

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

HTML CODE

<lightning-input name={item.id} type="radio" value="a" label={item.options.a} onchange={changeHandler}></lightning-input>

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

enter image description here

1 Answer 1

0

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);        
}
3
  • Thanks for your answer Derek, I removed additional declaration and keeping this and still getting the same error - const {name, value} = event.target; In short somehow this line is not working - const {name, value} = event.target; Mar 22, 2022 at 15:56
  • In the Console Log, I am actually getting the right values for name and value attributes Mar 22, 2022 at 15:58
  • Sorry about that. I think it worked I got the issue. In the Console Log, I should not be using - event.target.name or value anymore as I am using const {name, value} = event.target. Great help. Thanks Derek. Mar 22, 2022 at 16:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .