I'm trying to implement HTML5 datalist element in a easiest possible way.
Something like this:
<input list="browsers">
<datalist id="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
This does not work however. What would be my next step without having to install (npm) additional stuff.
Basically, I'm using the plain input react element and want to embed the datalist.
Here is my react code:
<input className={"custom_input inline "+this.isValidInteger(this.props.price.price_first,0,2000000)}
style={{marginRight:'5px'}}
value={this.props.price.price_first || ''} type="text"
onChange={(e)=>this.props.dispatch({type:"price", payload:e.target.value})}
placeholder=" Unesite..."
maxLength="10"/>
So I would like a dropdown on top of that.
React is very user-friendly and versatile, it is a popular programming language used today, but it operates under HTML5.
The datalist tag is introduced in HTML5. The <datalist> tag should be used with an <input< element that contains a "list" attribute. The value of "list" attribute is linked with the datalist id.
You can see that each browser displays a tick mark for each datalist option provided. Additionally, Chrome will snap the slider to these predefined values as the user moves the slider near them. Unfortunately, Internet Explorer and Chrome are the only browsers to support datalists on range inputs at this time.
HTML5 <datalist> elements cannot be styled.
I'm currently using the html5 datalist in react without issues.
Here goes the implementation:
<input type="text" list="data" onChange={this._onChange} />
<datalist id="data">
{this.state.data.map((item, key) =>
<option key={key} value={item.displayValue} />
)}
</datalist>
Datalists work fine in React, but you must close each option tag (with a backslash at the end).
<option value="something" />
It takes a little change to make MDN datalist example works in React.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/datalist
<label>
Choose a browser from this list:
<input list="browsers" name="myBrowser" />
</label>
<datalist id="browsers">
<option value="Chrome" />
<option value="Firefox" />
<option value="Internet Explorer" />
<option value="Opera" />
<option value="Safari" />
<option value="Microsoft Edge" />
</datalist>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With