2

i have this code written in html

<select id ='Font_Size' onchange="ChangeFont()">
            <option>Font Size </option>
            <option id ='sizeUp'>Large </option>
            <option id ='normal'>Normal</option>
            <option id ='sizeDown'>Small</option>
</select>

React.createElement('input',{type:'select',onChange:this.ChangeFont},React.createElement("input",{type:"Option"})) 

but i would like to create a react element that will create a selection field using the function React.createElement() and display it as the above code would. the code at the bottom is what i had tried but it does not work

4
  • I believe the element type should be select, not input. I think what you wrote would generate html something like <input type="select" onchange=... Commented Dec 6, 2018 at 19:56
  • i don't think i understand...what is want to do is rewrite the select tag and its options using the React.createElement() function Commented Dec 7, 2018 at 3:43
  • @mihiali I've added an answer demonstrating how to do it Commented Dec 7, 2018 at 15:38
  • Thank you Ted..ill try it out Commented Dec 8, 2018 at 18:07

1 Answer 1

5

What you were creating was an <input/>, you want to create a <select/>. Run the following snippet to see the code in action:

var App = React.createClass({
  render() {
    return React.createElement("select", {},
      React.createElement("option", {value: "A"}, "Option A"),
      React.createElement("option", {value: "B"}, "Option B"),
      React.createElement("option", {value: "C"}, "Option C")
    );
  }
});

ReactDOM.render(<App label="this is the label prop" />, document.getElementById('react'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.2/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/15.6.2/react-dom.min.js"></script>
<div id="react"></div>

Sign up to request clarification or add additional context in comments.

1 Comment

It Worked thanks Ted.. i didn't understand how to add the options

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.