I would like to take the option of which the user selects from a datalist. This is what I have:
<input type="text" name="my-input" class="searchBar" list="my-list">
<input id="clickMe" type="button" value="clickme" onclick="handleSelect();" />
<datalist id="my-list">
<option value="COMP232"></option>
<option value="COMP248"></option>
<option value="ENGR201"></option>
</datalist>
My JS method:
function handleSelect()
{
var elm = document.getElementById("my-list").innerHTML.value;
console.log(elm);
//window.location = "./class/" + elm.toUpperCase() + ".php";
}
The console responds "undefined" when I submit. What is wrong here?
You can assign an id
to input and take a value of id
on button click.
<input id= "inp" type="text" name="my-input" class="searchBar" list="my-list">
<input type="button" value="clickme" onclick="handleSelect();" />
<datalist id="my-list">
<option value="COMP232"></option>
<option value="COMP248"></option>
<option value="ENGR201"></option>
</datalist>
and JS code snippet will look like,
var elm = document.getElementById("inp").value;
Your code should be like this.
<script>
function handleSelect(){
var elment = document.getElementById("selectedValue").value;
console.log(elment);
}
</script>
<input id="selectedValue" type="text" name="my-input" class="searchBar" list="my-list">
<input id="clickMe" type="button" value="clickme"
onclick="handleSelect();" />
<datalist id="my-list">
<option value="COMP232"></option>
<option value="COMP248"></option>
<option value="ENGR201"></option>
</datalist>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.