0

I have a bunch of radio buttons with several duplicate attributes. Can I somehow assign all these in one place, maybe like [a, b, c] = (js)()? Thank you.

<input type="radio" value={`${index}-0`} 
                      key={`${index}-0`} 
                       id={`${index}-0`}
  checked={this.state.selections[index] === 0 }
  onChange={this.handleChange} />
<label htmlFor={`${index}-0`} className="pure-radio"> 0 </label>
2
  • 1
    Did you try to map all of them to the same model(variable)? Commented Aug 21, 2018 at 4:31
  • 1
    If you were using Angular, you can simply write <input type="radio" [value]="index0" [key]="index0" [id]="index0"> in your template file and change index0 in your js file. Commented Aug 21, 2018 at 4:34

2 Answers 2

1

Why not just assign them to the same variable? It's not quite what you are wanting, but better than your current situation.

var key = `${index}-0`;
...
<input type="radio" value={key} 
                  key={key} 
                   id={key}
                 checked={this.state.selections[index] === 0 }
                 onChange={this.handleChange} />
<label htmlFor={key} className="pure-radio"> 0 </label>
Sign up to request clarification or add additional context in comments.

Comments

0

All the attributes of an element is saved in attributes.

const attrs = document.getElementById('your_id').attributes

You can loop through them to access the attributes. See the example in the provided link.

Or, you can use getNamedItem to access desired attribute:

attrs.getNamedItem('id') // to get id attribute

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.