Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find specific text in a paragraph by line

Sorry if the question is worded weird but here is what I'm trying to do. I have to gather a specific line of info from a webpage. What I am having trouble with is I can not figure out the right selector to use, eq() is not working . Here is the source from the page I'm trying to get the info from.

<div class="info" id="Data">
Domain Name: example.com
<br>
Registry: 12345
<br>

When I inspect element I get div#Data.info (text) I have tried $('div#info.Data').eq(1).text() and a few other combinations. I an new to scripting and the (text) part is what is I think is throwing me off.

like image 212
Kevin h Avatar asked Dec 19 '22 22:12

Kevin h


1 Answers

Let's look at your jQuery:

$('div#info.Data') // Gets <div> with id="info" and class="Data"
                   // ^ You have id and class reversed!
.eq(1)             // This gets the 2nd element in the array
                   // ^ You only tried to get 1 element. What is the 2nd?
.text()            // Returns combined text of selected elements

Also there's another issue. Your text is not in its own element. In order to get the textnodes in your current element, you could call .contents(). jQuery does not treat text nodes like regular elements, though, so I would be careful doing any additional operations on them.

You can retrieve the text like such:

$("#Data").contents().eq(0).text() // -> 'Domain Name: example.com'
$("#Data").contents().eq(2).text() // -> 'Registry: 12345'

Fiddle

like image 116
Stryner Avatar answered Jan 05 '23 11:01

Stryner