Shah Rushabh Shah Rushabh - 29 days ago 16
HTML Question

How to get data value from element class?

HTML code:

<input type="file" name="efile" id="efile" data-cat="electronics" style="width:100%" class="alertme">
<input type="file" name="dfile" id="dfile" data-cat="Decoratives" style="width:100%" class="alertme">
<input type="file" name="kfile" id="kfile" data-cat="kitchen" style="width:100%" class="">
<input type="file" name="hdfile" id="hdfile" data-cat="Home" style="width:100%" class="">
<input type="file" name="ffile" id="ffile" style="width:100%" data-cat="Furnitures" class="alertme">


How can I get data-cat attribute value where class alertme is exist? and thanks for reading my question. I am weak in English so please apologize me if I made any grammatical or spelling mistakes.

Answer

you can try like this

$(document).ready(function(){
  $(".alertme").each(function(){
 var val = $(this).data('cat');
    alert(val);
});
})
<input type="file" name="efile" id="efile" data-cat="electronics" style="width:100%" class="alertme">
<input type="file" name="dfile" id="dfile" data-cat="Decoratives" style="width:100%" class="alertme">
<input type="file" name="kfile" id="kfile" data-cat="kitchen" style="width:100%" class="">
<input type="file" name="hdfile" id="hdfile" data-cat="Home" style="width:100%" class="">
<input type="file" name="ffile" id="ffile" style="width:100%" data-cat="Furnitures" class="alertme">
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>