0

I want to know is it possible to access the next or prev element in ReactJs?I have two inputs by labels a and b.In handlerChecked function I want to check the attribute checked of both inputs.For example I checked a and I want also to check b was checked or not. How can I do it?

class App extends React.Component {
 constructor(props) {
    super(props);
    this.state = {
        data: [],
        .
        .
        .
    }
}
       .
       .
       .

handlerChecked = (e, elem) => {
    let isChecked = e.target.checked
    let value = e.target.value
    let checknext = e.target.next('input').checked //In this part I want to check the attribute checked of other Input//// 
}

.
.
.
render() {
    return (
        <div>
            <input type="checkbox" className="" value="0" onChange={this.handlerChecked} /> <label><span>a</span></label>
            <input type="checkbox" className="" value="1" onChange={this.handlerChecked} /> <label><span>b</span></label>

        </div>
    )
}
}
ReactDOM.render(<App />, document.getElementById('Result'))
1
  • You should not work with DOM Elements directly. It's not how React works. What you should do is to use state and props to handle VirtualDOM updates. React will make all DOM updates for you. Commented Apr 27, 2019 at 7:27

1 Answer 1

3

The short answer is no, React doesn't work that way, you can't go up and down an element's ancestor's tree to find sibling elements.

A common way to implement this feature is to give each checkbox a name attribute.

For example:

  <div>
    <input name="check1" type="checkbox"  onChange={this.handlerChecked} checked={this.state.check1} /> <label><span>a</span></label>
    <input name="check2" type="checkbox" onChange={this.handlerChecked} checked={this.state.check2} /> <label><span>b</span></label>
</div>

handle these two checkboxes with one handler

const handlerChecked = (e) => {
  const {name, checked} = e.target
  this.setState({
    [name]: checked
  })
}

then in your state keep track of those 2 names like this:

this.state = {
 check1: false,
 check2: false
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @mxdi9i7 for your answer , But I did not get how I can check the attribute checked of each input by your code.
The point is to have state properties for the all checkboxes. You shouldn't work with DOM elements directly, you're killing all React's optimizations for render and reconciliation process.
@sahar when you invoke the onChange listener on the checkbox, the callback function handlerChecked will be called with a parameter e, that's the event of change. From that e you can find the target and thus the checked value of that checkbox. You can see the implementation from the second code block.
@sahar glad I helped! Kindly accept my answer if it solves your question.

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.