Parth Mane Parth Mane - 1 year ago 63
Javascript Question

I want to obtain a certain part of text from a large webpage using Javascript, how do I?

There is a certain webpage which randomly generates a number, for example

"Frequency : 21"
. I am trying to create a script which takes the number,
, and compares it to another variable, then to an
if else
function. Basically, I've completed most of it, but I can't obtain the number
. And since it is random, I can't put in a fixed value.

Can anyone help me out?

My code goes like:


function MyFunction(level,legmin) {
var level = x
var legmin = 49
if (level <= legmin) {
else {
alert("Met requirements.")

where the address of the text I want is:

div#scroll>div#scrollContent>div>div>div#pkmnappear>form>p (x in the code above).

Answer Source

A quick-n-dirty solution without regex.

var lookFor = "Frequency : ";
var text = document.querySelector("#pkmnappear>form>p").textContent;
var level = text.substr(text.indexOf(lookFor) + lookFor.length).split(" ")[0];

This assumes the number will be followed by a space