9

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?

2
  • 1
    Code like what? Give a proper code example or it will be hard to understand what you actually mean. Commented Jul 26, 2012 at 9:44
  • i am new to stack overflow. that's why such mistake happened Commented Jul 26, 2012 at 11:05

3 Answers 3

7

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

Sign up to request clarification or add additional context in comments.

Comments

6
$('.a')[0].innerHTML;
$('.a')[1].innerHTML;

FIDDLE

1 Comment

Iam wondering when he wanted to use this method then why don't he use Javascript only
0

Try this:

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

demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.