1

I have a dropdown i want to display a div as per gender as given below using jquery, this is working fine but how can i do it in reactJs?

$(function(){
  $('select').change(function(){
  var selectedValue = $(this).find(':selected').val();
   $('.myDiv').hide();
  $('.' + selectedValue).show();
  });
});
.myDiv{ display:none;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select>                                             <option>Gender</option>
<option value="gents-content">Gents</option>
<option value="ladies-content">Ladies</option>
</select>

<p class="myDiv gents-content">getns content</p>
<p class="myDiv ladies-content">ladies content</p>

ReactJs Code==========================================================

  constructor(props){
        super(props)
        this.state = {value: 'show'}
    }
    
      handleGender = event => {
        this.setState({value: event.target.value})
    }
    
     render() {
        return (
            <div>
<select className="form-control genderList" name="" onChange={this.handleGender}>                                         <option>Gender</option>
<option value="show">Gents</option>
<option value="hide">Ladies</option> 
</select>                                                    
                                                    
<div className={this.state.value}>This is div</div>
</div>

ThankYou!

2 Answers 2

1

You don't need to select anything in React. With a state definition you can achieve your request. I changed and wrote this code based on your React code:

 constructor(props){
    super(props)
    this.state = {value: ''}
}

handleGender = event => {
    this.setState({value: event.target.value})
}

render() {
    let myDiv = this.state.value =='show' ? <div class="gents-content">getns content</div> :
                this.state.value =='hide' ? <div  class="ladies-content">ladies content</div> : 
                <div></div>;
    return (
        <div>
        <select className="form-control genderList" name="" onChange={this.handleGender}>
           <option>Gender</option>
           <option value="show">Gents</option>
           <option value="hide">Ladies</option> 
        </select>                                                                                                        
       {myDiv}
       </div>
}
Sign up to request clarification or add additional context in comments.

5 Comments

function is not working in this because this is class based reactJs
'state' is not defined no-undef
@RohitVerma Excuse me, it should be this.state.value not state.value lonely.
this is working now but when I select heading "Gender" then both div should be hide...
@RohitVerma for its default value, just set the state's default value to ''. I updated the state definition.
1

You could display the value of the state object. Basically move this.state.value from the css class to between the <div> and change the value from show and hide to gent and lady (or whatever you want). Also add an empty value for the default option to hide the <div />:

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = { value: "" };
  }

  handleGender = (event) => {
    this.setState({ value: event.target.value });
  };

  render() {
    return (
      <div>
        <select
          className="form-control genderList"
          name=""
          onChange={this.handleGender}
        >
          <option value="">Gender</option>
          <option value="gent">Gents</option>
          <option value="lady">Ladies</option>
        </select>

        {this.state.value && <div>This is the gender: {this.state.value}</div>}
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById("app"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="app" />

2 Comments

i not need to display only selected value. I need to display other data but those data should be as per selected value...
That is no problem, just exchange this.state.value for this.state.value =="gent" ? someComponent : this.state.value == "lady" ? someOtherComponent : defaultComponent

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.