Link to home
Start Free TrialLog in
Avatar of MJ
MJFlag for United States of America

asked on

Detect Phrase on a Web-page?

I'm trying to figure out how I can scrap/detect if the phrase "We regret we cannot open the account you requested." is on the page using CSS Selector and/or Javascript (jQuery is ok if that is the only option but want to avoid it if possible?). Below is an excerpt from the mark-up.
<!-- current page: offers --> 
        <div class="main">
     <h3>Eligibility Results</h3><p>Thank you for your online application for an account at Bank of the West.  We regret we cannot open the account you requested.<br/>
If you would like to discuss this application with Bank of the West, please contact us at:</p>

Open in new window


Thanks!
Avatar of Steve Bink
Steve Bink
Flag of United States of America image

This isn't possible with CSS.

With Javascript, you can use document.getElementById() to target the parent container.  Then get that element's innerText property to search for your text:
<script>
  var my_element = document.getElementById('main');
  if (my_element.innerText.search('We regret we cannot open the account you requested') > -1) {
    // text is found
  }
  else {
    // text is not found
  }
</script>

Open in new window


https://developer.mozilla.org/en-US/docs/Web/API/Node/innerText
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/search
Avatar of MJ

ASKER

Just realized main is a class and NOT an ID! :0)
ASKER CERTIFIED SOLUTION
Avatar of Steve Bink
Steve Bink
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MJ

ASKER

try{
    var appDeclined = 0;
    var myCollection = document.getElementsByClassName('main');
    i = myCollection.length;

    while(i--) {
        if (myCollection[i].innerText.search('We regret we cannot open the account you requested') > -1) {
            appDeclined = 1;
        }
    }
  
    
  
} catch(e) {} 

Open in new window