How to get tag in html page, if I know what text tag contains. E.g.:
<a ...>SearchingText</a>
How to get tag in html page, if I know what text tag contains. E.g.:
<a ...>SearchingText</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`.
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.
I think you'll need to be a bit more specific for us to help you.
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.