views:

601

answers:

4

Hello,

It's probably something really simple, but I'm just learning.

There's a page with 3 blockquote tags on it, and I'd need to get the innerHTML of the one containing a certain string. I don't know how to search/match a string and get the innerHTML of the tag containing the matched result.

Any help would be appreciated for this newbie! Thanks, -Chris

+1  A: 

Assign an id to the blockquote elements then you can get the innerHTML like this:

HTML:

<blockquote id="bq1">Foo</blockquote>

JS:

var quote1 = document.getElementById('bq1').innerHTML;
Darrell Brogdon
It's not my page, I can't assign an ID, but thanks for the reply anyways! :)
Salaman
+3  A: 
var searchString = 'The stuff in innerHTML';
var elements = document.getElementsByTagName('blockquote')
for (var i = 0; i < elements.length; i++) {
     if (elements[i].innerHTML.indexOf(searchString) !== -1) {
         alert('Match');
         break;
     }
}

:)

Btw there would be a much nicer method if you'd be using Prorotype JS (which is much better than jQuery btw):

var el = $$('blockquote').find(function(el) {
    return el.innerHTML.indexOf('The string you are looking for.') !== -1;
});

You could of course also use regular expressions to find the string, which might be more useful (use el.match() for that).

watain
This will only match the element which's innerHTML is exactly like searchString, and that is not what he wants (why would you search for it then, if you already have the string)?
Jan Hančič
Whoops yeah, looks like I failed :D. Going to correct it.
watain
You are just returning true/false in the last example :)
Jan Hančič
Ehm, no? It should return the matched element. I just tested it ;).
watain
I tried it and it works perfectly :D Thanks a lot.
Salaman
"which is much better than jQuery btw" ... Don't state it like a fact -- it's not one.
J-P
@watain: oh sorry, the `find` method returns the current element if the function you pass it to returns true. My bad :)
Jan Hančič
You should be caching `elements.length`
J-P
+4  A: 

If you need to search through every <blockquote> on the page, try this:

function findBlockquoteContainingHtml(matchString) {
    var blockquoteElements = document.getElementsByTagName('BLOCKQUOTE');
    var i;
    for (i = 0; i < blockquoteElements.length; i++) {
        if (blockquoteElements[i].innerHTML.indexOf(matchString) >= 0) {
            return blockquoteElements[i].innerHTML;
        }
    }
    return null;
}
Kevin Conner
For some reason I couldn't get this to work (even after replacing the last })
Salaman
+2  A: 

Be careful using innerHTML to search for text within a tag, as that may also search for text in attributes or tags as well.

You can find all blockquote elements using:

var elems = document.getElementsByTagName("blockquote")

You can then look through their innerHTML, but I would recommend instead looking through their textContent/innerText (sadly, this is not standardized across browser, it seems):

for (i in elems) {
  var text = elems[i].textContent || elems[i].innerText;
  if (text.match(/foo/)) {
    alert(elems[i].innerHTML);
  }
}
Brian Campbell