0

I want to get the data inside "opt_id". And the same code works when i use selelct tag insted of datalist.

This is how my code looks like:

var get_data = document.getElementById("select_opt");
var dataIndex = get_data.options[get_data.selectedIndex].getAttribute("opt_id");
<input id="optio_lists" list="options" placeholder="Search...">
<datalist id="select_opt">
<option opt_id="0"  label="Cricket">Cricket</option>
<option opt_id="1"  label="Football">Football</option>
<option opt_id="2"  label="Tennis">Tennis</option>
<option opt_id="3"  label="Basketball">AKITA</option>
</datalist>

2
  • Typo: "select_op" should be "select_opt" Commented Mar 11, 2021 at 10:16
  • 4
    opt_id is not a valid attribute. Use a data-* attribute for this -> data-opt_id Commented Mar 11, 2021 at 10:18

2 Answers 2

1

In your script tag in the get_data variable at document.getElementById() you have misspelled the id - instead of select_opt you have written select_op.

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

Comments

0

Data list is just a source of data and is different from a select box. Hence selectedIndex property doesn't makes sense here.

Check this link for more details - Is there a SelectedIndex for an HTML5 DataList?

1 Comment

Yes, i got that. But the suggested one option is not giving me the value inside that "attribute(opt_id)". That is working fine in the "value" case. Thanks for your time !

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.