Arun
Arun

Reputation: 1472

set value for span using jquery

I have an <li> which have a field named data-resultcount.I need to select the data-resultcount value and print in the span.My <li> is

  <li id="totalCount" style="display:none" data-total="19" data-resultcount="19">totalCount</li>

and the <span> is

<div class="resultCount">Results:
    <span></span>
</div>

Thanks in advance for help

Upvotes: 0

Views: 2756

Answers (7)

Ravi
Ravi

Reputation: 853

Try the following:

$(".resultCount").find('span:first').text($("#totalCount").attr('data-resultcount'))

Fiddle Sample

Upvotes: 1

Suhas Gosavi
Suhas Gosavi

Reputation: 2170

Try this

$(".resultCount").find('span').text($("#totalCount").attr('data-resultcount'))

Working Fiddle

Upvotes: 0

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85545

Use prop()method for better result:

$('.resultCount span').text($('li#totalCount').prop('data-resultcount'));

Upvotes: 1

Subin
Subin

Reputation: 3563

This will work :

$('.resultCount span').text($('li#totalCount').attr('data-resultcount'));

Upvotes: 1

Nitin Varpe
Nitin Varpe

Reputation: 10694

Try

$('.resultCount span').text($('#totalCount').attr('data-resultcount'));

Upvotes: 1

Satpal
Satpal

Reputation: 133403

You can use .data() to fetch value from data-resultcount

$('.resultCount').find('span').text($('#totalCount').data('resultcount'))

or

$('.resultCount span').text($('#totalCount').data('resultcount'))

Upvotes: 2

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

Try this:

html:

<div class="resultCount">Results:
    <span id ="mySpan"></span>
</div>

js:

document.getElementById("mySpan").innerHTML="Span text is changed";

Hope this helps.

Upvotes: 2

Related Questions