html value 1

How to get innerHTML for a specific html element through its class in jQuery?

I have an HTML code:

<div class="a">html value 1</div>

<div class="a">html value 2</div>

How can I access html value 1and html value 2using jquery?

+5
source share
4 answers
$('.a')[0].innerHTML;
$('.a')[1].innerHTML;

Fiddle

+3
source

Separately:

$('div.a:eq(0)').html(); // $('div.a:eq(0)').text();
$('div.a:eq(1)').html(); // $('div.a:eq(1)').text();

Using a loop:

$('div.a').each(function() {
   console.log( $(this).html() ); //or $(this).text();
});

Using .html()

​$('div.a').html(function(i, oldHtml) {
  console.log( oldHtml )
})​​;

Demo

Using .text()

$('div.a').text(function(i, oldtext) {
  console.log( oldtext )
})​;

Demo

+6
source

Based only on the class, as he asked:

$('.a').each(function() {
    console.log($(this).html());
});
0
source

Try the following:

var a = document.getElementsByClassName('a');
for (var i = 0; i < a.length; i++) {
    alert(a[i].innerHTML)
}

demo

0
source

All Articles