user3772251
user3772251

Reputation: 77

How to get specific span's value from div in JQuery

I have a div and i want to get text of a specific span from that div. Following is my Div code:

'<div class="dvDynamic_' + pid + '"><p hidden="true">'+pid+'</p><span class="count_' + pid + '">' + count + '</span><span id="pname" style = "margin-left:70px;">' + pname + '</span><span id="punitprice" style = "margin-left:150px;">' + uprice + '</span></div>' 

and i want to get text of the following span:

<span class="count_' + pid + '">' + count + '</span>

Please help me how to do it .

Upvotes: 0

Views: 83

Answers (5)

Jakir Hossain
Jakir Hossain

Reputation: 2517

Get span element to target have next sibling with id. you can get that element using id selector and traverse to required span using .prev():

$('#pname').prev().html();

or

$('#pname').prev().text();

Upvotes: 1

Pratik
Pratik

Reputation: 954

Using find you can get value of span through div

function findSpanValue(pid){
    return $(".dvDynamic_"+pid).find(".count_"+pid).text();
}

Upvotes: 1

Milind Anantwar
Milind Anantwar

Reputation: 82241

The span element which you want to target have next sibling with id. you can target that element using id selector and traverse to required span using .prev():

$('#pname').prev().text()

Upvotes: 2

Satpal
Satpal

Reputation: 133403

You can identify pname element using ID selector then use .prev() to identify the desired span

$('#pname').prev().text()

Upvotes: 1

Swaraj Giri
Swaraj Giri

Reputation: 4037

If you know the pid of the span you want to get, you can use

$('.count_' + pid).text()

Upvotes: 1

Related Questions