2

I am trying to grab a string from a HTML page. This string lives inside a div tag with no ID, and has an ever changing title property.

it looks something like this:

<div title = [this title changes depending on how the page is pulled up]>
EmailAddress abc@xyz
</div>

I want to be able to grab "abc@xyz" out of this whole mess.

This HTML document is ever changing, the only thing I know for sure that stays the same is that the string I want to grab will always be preceded by "EmailAddress"

I've been staring at this for 3 hours with no progress. I'd be very thankful if someone can point me in the right direction.

2
  • have you tried using substring() Commented May 21, 2013 at 6:24
  • 5
    What evil stuff do you want to do with those e-mails? Commented May 21, 2013 at 6:29

4 Answers 4

4

Without jQuery:

var divElements = document.getElementsByTagName( 'div' );

for ( var i = 0; i < divElements.length; i++ ) {
    if ( divElements[i].innerText.match( 'EmailAddress' ) ) {
        // your div 
        var mail_id =  divElements[i].innerText.replace('EmailAddress ','');
    }
}

See this fiddle.

Sign up to request clarification or add additional context in comments.

Comments

0

If you don't have any way of narrowing down the containing element via an ID, then you can try reading in the body HTML and using regex to retrieve the text:

var html = document.getElementsByTagName('body')[0].innerHTML;
var result = html.match(/EmailAddress\s([^<]+)</i)[1];

This is a very rudimentary regular expression, but should do the job to start with.

Comments

0

Try this

var x = document.querySelectorAll('div[title]'), email;
if(x.length){
    for(var i = 0; i < x.length; i++){
        var inner = x[i].textContent || x[i].innerText;
        if(/EmailAddress/.test(inner)){
            email = inner.substring(inner.indexOf('EmailAddress') + 13);
            email = email.replace(/\s.*$/, '')
            break;
        }
    }
}

Demo: Fiddle

Comments

-1
$('div').each(function(){
    var divText=$(this).text();
    var ok=divText.contains('EmailAddress')
    if(ok){
      alert(divText);
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.