Nitesh kumar
Nitesh kumar

Reputation: 13

Getting data(text) from HTML elements(h2/span) of a webpage for Web Application

I am building a web application to get data from html page and I need the data of span inside the div class

Example:

<h2 class="doctor-specialties">
<span>
<a class="link grey" href="https://www.practo.com/delhi/dentist">Dentist</a>
</span>
, 14 Years Experience
</h2>

I want the data within <span> which is "Dentist" and the next value "14 years experience" which is outside the span but both the elements in the same class.

As follow:

Dentist ,14years experience

Upvotes: 0

Views: 51

Answers (2)

depperm
depperm

Reputation: 10756

One way is to use the text() method and then get rid of any unneeded whitespace.

$(function(){
    var string=$('.doctor-specialties').text().replace(/\s{2,}/g,"");
	console.log(string);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2 class="doctor-specialties">
<span>
<a class="link grey" href="https://www.practo.com/delhi/dentist">Dentist</a>
</span>
, 14 Years Experience
</h2>

Upvotes: 1

charlietfl
charlietfl

Reputation: 171690

Can simply get the text of the h2:

alert( $('h2.doctor-specialties').text() );

Upvotes: 0

Related Questions