简体   繁体   中英

get data-attribute from function

I´m trying to get the data-io attribute from my function.

<input id="42297" class="edit flatpickr-input active" data-io="in" value="14:23" type="text" readonly="readonly">

onChange: function(selectedDates, time, instance){

With this code i can get my input id threw: instance.input.id
But i cannot figure out how to access the data-io attribute.

I´ve tried:

instance.input.data-io
instance.input.data('io')
instance.input.attr('data-io')
instance.input.attr('io')

How should i do?

Use instance.input.dataset.io . More information about data-* attributes here: https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes

Since using jQuery use data-api:

$('#42297').data('io'); // -> in

Keep in mind that id should start with letter.


If your instance.input is JS object, convert it to jQuery: $(instance.input).data('io')

If you are using jquery use either .data('io') or attr('data-io') . If using vanilla js, utilise Element.getAttribute() .

 var inp = document.getElementById('42297'); console.log(inp.getAttribute('data-io')); var $inp = $('#42297'); console.log($inp.data('io')); console.log($inp.attr('data-io')); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="42297" class="edit flatpickr-input active" data-io="in" value="14:23" type="text" readonly="readonly"> 

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM