views:

27

answers:

3

How to get tag in html page, if I know what text tag contains. E.g.:

<a ...>SearchingText</a>
+1  A: 

You'll have to traverse by hand.

var aTags = document.getElementsByTagName("a");
var searchText = "SearchingText";
var found;

for (var i = 0; i < aTags.length; i < il) {
  if (aTags[i].innerHTML == searchText) {
    found = aTags[i].innerHTML;
    break;
  }
}

// Use `found`.
August Lilleaas
innerHTML is not standard, I'd use textContent and innerText.
AutoSponge
A: 

While it's possible to get by the inner text, I think you are heading the wrong way. Is that inner string dynamically generated? If so, you can give the tag a class or -- better yet -- ID when the text goes in there. If it's static, then it's even easier.

Zack
A: 

I think you'll need to be a bit more specific for us to help you.

  1. How are you finding this? Javascript? PHP? Perl?
  2. Can you apply an ID attribute to the tag?

If the text is unique (or really, if it's not, but you'd have to run through an array) you could run a regular expression to find it. Using PHP's preg_match() would work for that.

If you're using Javascript and can insert an ID attribute, then you can use getElementById('id'). You can then access the returned element's attributes through the DOM: https://developer.mozilla.org/en/DOM/element.1.

Jeff Meyers