Find specific text line by line

Sorry if the question is worded weird, but here's what I'm trying to do. I need to collect a specific line of information from a web page. I am having problems with the fact that I cannot figure out which correct selector to use eq()does not work. Here is the source from the page I'm trying to get information on.

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

When I check an element, I get a div # Data.info (text) I tried $('div#info.Data').eq(1).text()several other combinations. I'm new to scripting and the (text) part - this is what I think it throws me off.

+4
source share
2 answers

Take a 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

. . , .contents(). jQuery , , .

:

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

Fiddle

+9

, id class , .

, , <br />.

.

$(document).ready(function() {
  var content = $('div#Data.info').eq(0).text();
  var lines = content.split("\n").filter(Boolean)
  console.log(lines);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="info" id="Data">
Domain Name: example.com
<br>
Registry: 12345
<br>
</div>
Hide result
+2

Source: https://habr.com/ru/post/1617466/


All Articles