Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have HTML code like this:

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

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

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

like image 310
EbinPaulose Avatar asked Jul 26 '12 09:07

EbinPaulose


1 Answers

Separately:

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

Using 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

like image 147
thecodeparadox Avatar answered Oct 20 '22 04:10

thecodeparadox