0

how to get the class name of divs?

<body>
    <a id="append">Add DIV</a> 
    <div id="parent"></div>
    <script>
        $(document).ready(function() {
          var count = 0;
          $('#append').click(function() {
            $('#parent').append('<div
                class="article' + count + '">im div</div>');
            count++;
          });
          $('div').click(function() {
            alert(this.class);
          });
        });
    </script>
</body>

when i am trying to display class name of divs in alert it shows undefind.

Hitesh Misro
  • 3,397
  • 1
  • 21
  • 50
kumar
  • 207
  • 1
  • 3
  • 9

8 Answers8

1

There is an attr property:

$(this).attr('class');
mtch9
  • 298
  • 2
  • 11
1

This is Example Of Your Question

$(document).ready(function(){
alert($('#ss').attr('class'));
});
.ssclass{
width:100%;
  height:100vh;
  background:#ff8800;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="ss" class="ssclass"></div>
Samudrala Ramu
  • 2,088
  • 1
  • 15
  • 36
1

Try this...

$(document).ready(function() {
  var count = 0;
  $('#append').click(function() {
    $('#parent').append('<div class="article' + count + '">im div</div>');
    count++;
  });
  $('div').click(function() {
    alert($(this).attr('class'));
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<body>
  <a id="append">Add DIV</a> 
  <div class='className' id="parent"></div>
</body>
Sankar
  • 6,908
  • 2
  • 30
  • 53
1

Use event delegation for dynamically created elements. Otherwise your class not triggered on click.

$('#parent').on('click', 'div', function() {
  var getclass = $(this).attr('class');
  console.log(getclass);
});
Sudharsan S
  • 15,336
  • 3
  • 31
  • 49
1

Currently what you are using is called a "direct" binding which will only attach to element that exist on the page at the time your code makes the event binding call. As you are generating elements dynamically, You need to use Event Delegation using .on() delegated-events approach.

General Syntax

$(document).on('event','selector',callback_function)

Additionally, You need to read className property or use getAttribute()

$(document).ready(function() {
  var count = 0;
  $('#append').click(function() {
    $('#parent').append('<div class="article' + count + '" > im div </div>');
    count++;
  });
  $('#parent').on('click', "div", function() {
    alert(this.className)
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a id="append">Add DIV</a> 
<div id="parent"></div>
Satpal
  • 132,252
  • 13
  • 159
  • 168
1

this is how you can get the all the class name of div's. refer the working demo.

<!DOCTYPE html>
<html>
<head>
 <title></title>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
 
</head>

<style type="text/css">




</style>


<body>

<div class="main">
 <div class="classone">my name is class one</div>
 <div class="classtwo">my name is class two</div>
 <div class="classthree">my name is class three</div>
</div>

<script type="text/javascript">

$(document).ready(function(){
 
 var thelength = $("div.main div").length;//get how many divs
 for(var i=0;i<thelength;i++)
 {
  var theclass_name = $("div.main div").eq(i).attr('class');//get the class name of each.
  alert(theclass_name);
 }
});

</script>




</body>


</html>
caldera.sac
  • 4,918
  • 7
  • 37
  • 69
0

Use it

 <body> 
  <a id="append">Add DIV</a> 
  <div id="parent"></div>
  <script>
    $(document).ready(function() { 
      var count = 0; 
      $('#append').click(function(){    
        $('#parent').append('<div class="article'+count+'">im div</div>'); count++; 
      }); 
      $('#parent div').click(function(){
        alert($(this).attr('class'));  
      }); 
    }); 
  </script>
</body>
Komal
  • 2,716
  • 3
  • 24
  • 32
0

Use Event delegation heres the Fiddle

$('#parent').on('click','div',function() {
className = $(this).attr('class');
    alert(className);
});
Vivekraj K R
  • 2,418
  • 2
  • 19
  • 38