0

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.

Shah Rushabh
  • 147
  • 4
  • 16

5 Answers5

2

here is code to achieve this:

$(".alertme").each(function(){
    // Here you can fill your array or whatever you want..
    $(this).attr("data-cat"); 
});
danish farhaj
  • 1,316
  • 10
  • 20
0

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>
Bharat
  • 2,441
  • 3
  • 24
  • 36
0

Use attr() to get the data attribute and a loop do push the value to the data array

var data = [];
$(".alertme").change(function() {
  $(".alertme").each(function() {
    data.push($(this).attr("data-cat"));
  });
  console.log(data);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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">
Graham
  • 7,431
  • 18
  • 59
  • 84
madalinivascu
  • 32,064
  • 4
  • 39
  • 55
0

First you have to get all elements with that class:

var aAlertMe = window.document.getElementsByClassName("alertme");

Secondly, for each element in the array insert its data into another array:

var aDataValues = [];
for(var i = 0; i < aAlertMe.length; i++){
    aDataValues.push(aAlertMe[i].dataset.cat);
}

And you'll have all the values inside aDataValues.

xale94
  • 424
  • 5
  • 12
0

Try This :

$("input[type='file']").each(function () {
    if($(this).hasClass("alertme"))
        alert($(this).data("cat"));
});
Amit Maru
  • 132
  • 7