Ibrahim Azhar Armar
Ibrahim Azhar Armar

Reputation: 25763

how to fetch the text of a div using jquery?

How do i fetch the text within the div using id selector i.e [email protected] in this case using jQuery.

<div class="g10 edit_trader" id="email_54">[email protected]</div>

the following code fetch the value email_54

$('edit_trader').attr('id')

i tried using

$('edit_trader').attr('id').val();
$('edit_trader').attr('id').text();
$('edit_trader').attr('id').html();

nothing works.

thank you..

Upvotes: 0

Views: 214

Answers (3)

bozdoz
bozdoz

Reputation: 12890

Classes have periods in front of them. If the IDs are dynamic, call it with .edit_trader[id]. The [id] will check for elements with class edit_trader and an ID. This is in case you have multiple elements with class edit_trader. Also, if you know what the dynamic IDs start with, you could use [id^="email"], for example.

This should work:

$('.edit_trader[id]').text()

See the jsfiddle.

Upvotes: 0

Moin Zaman
Moin Zaman

Reputation: 25465

$('#email_54').html();

That's what you want

or

$('#email_54').text();

or if you're trying to construct the ID then get the text then do this:

var fooID = $('edit_trader').attr('id');
var fooText = $('#'+fooID).text();

or if you have many elements then you will need to loop or use .each() like this:

$('.edit_trader').each(funciton(i){
    var fooID = $(this).attr(id);
    var fooText = $('#'+fooID).text();
    //do  stuff now that you have the text in fooText
});

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1039498

An id selector is prefixed with #. So if you wanted to get the contents of an element with id = email_54 you could do this:

var text = $('#email_54').text();

And if you wanted to use a class selector you should prefix with .:

var text = $('.edit_trader').text();

But of course in this case since you could have multiple elements with the same class you might get an array as a result.

Upvotes: 1

Related Questions