Reputation: 51
I have a problem trying to obtain text between a certain set of <p>
tags using JQuery - here is the html that I currently have:
<div class="mainarea">
<p>
<strong>Location: </strong>
</p>
<!-- display search results -->
<p>
You searched for
<strong>"xyz"</strong>.
</p>
I'm trying to get the text within the second set of <p>
tags (You searched for ...), but I keep getting the text from the first <p>
tag (Location ...).
So far I have this function:
$('.mainarea p')
, which returns "Location..." and have tried to add the .text() function to it, but this doesn't seem to help.
Upvotes: 1
Views: 770
Reputation: 308
Similarly we can use the following code given bellow
console.log("value is"+$('.mainarea p:last').html()) ;
Here your p position at last so i am using this approached.If p is not last element then we can use .eq()to set the index or get the index.
Upvotes: 0
Reputation: 87203
Use eq() or :eq selector
$('.mainarea p').eq(1).text();
Using selector:
$('.mainarea p:eq(1)').text();
Note: Used .text() as mentioned in the text. To get innerHTML use .html()
$('body').html($('.mainarea p').eq(1).html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<div class="mainarea">
<p>
<strong>Location: </strong>
</p>
<!-- display search results -->
<p>
You searched for <strong>"xyz"</strong>.
</p>
Upvotes: 4