3

$("input[name=TypeList]").focusout(function(){
    alert($(this).attr('data-value'));
});
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input  id="s" name = "TypeList" list = "TypeList" placeholder = "Select Type"/>
<datalist id = "TypeList">
   <option  data="1" data-value="1">Internet Explore1</option>
   <option data-value="4">Internet Explore4</option>
   <option data-value="3">Internet Explore3</option>
   <option data-value="2">Internet Explore2</option>
</datalist>
</body>

0

4 Answers 4

2

Not sure if there is any easier solution than this. Try below.

$("input[name=TypeList]").focusout(function(){
     console.log($([].slice.call($(this)[0].list.children).filter((e) => {     
     
     return e.value === $(this).val();
     })).attr('data-value'));
});
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input  id="s" name = "TypeList" list = "TypeList" placeholder = "Select Type"/>
<datalist id = "TypeList">
   <option  data="1" data-value="1">Internet Explore1</option>
   <option data-value="4">Internet Explore4</option>
   <option data-value="3">Internet Explore3</option>
   <option data-value="2">Internet Explore2</option>
</datalist>
</body>

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

Comments

2

You can use jQuery :contains() pseudo-class selector to select based on content.

$("input[name=TypeList]").focusout(function(){
    alert($(`#TypeList option:contains('${this.value}')`).data('value'));
});
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input  id="s" name = "TypeList" list = "TypeList" placeholder = "Select Type"/>
<datalist id = "TypeList">
   <option  data="1" data-value="1">Internet Explore1</option>
   <option data-value="4">Internet Explore4</option>
   <option data-value="3">Internet Explore3</option>
   <option data-value="2">Internet Explore2</option>
</datalist>
</body>

For an exact match, you have to use the filter() method.

$("input[name=TypeList]").focusout(function() {
  var val = this.value;
  alert($('#TypeList option').filter(function() {
    return val === this.value;
  }).data('value'));
});
<body>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <input id="s" name="TypeList" list="TypeList" placeholder="Select Type" />
  <datalist id="TypeList">
   <option  data="1" data-value="1">Internet Explore1</option>
   <option data-value="4">Internet Explore4</option>
   <option data-value="3">Internet Explore3</option>
   <option data-value="2">Internet Explore2</option>
</datalist>
</body>

FYI : For getting data-* value use jQuery data() method.

Comments

0

$(this).val() should do the job for you.. :)

$("input[name=TypeList]").focusout(function(){
    alert($(this).val());
});
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input  id="s" name = "TypeList" list = "TypeList" placeholder = "Select Type"/>
<datalist id = "TypeList">
   <option  data="1" data-value="1">Internet Explore1</option>
   <option data-value="4">Internet Explore4</option>
   <option data-value="3">Internet Explore3</option>
   <option data-value="2">Internet Explore2</option>
</datalist>
</body>

Comments

0

There is another way to do this by reacting on the change event of the input value:

$(function(){
    $('#s').change(function(){
        console.log($("#TypeList option[value='" + $('#s').val() + "']").attr('id'));
    });
});

Comments

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.