Vlad Holubiev
Vlad Holubiev

Reputation: 5154

How to get a tag inner using getElementById after specified item?

How to get a tag inner-text of <a>-tag that are next after red span?

Here's example source code:

<p>
    Text1<br>
    <span style="color:blue">span</span><br>
    <a href="http://google.com">Google</a>
</p>
<p>
    Text1<br>
    <span style="color:red">span</span><br>
    <a href="http://bing.com">Bing</a>
</p>
<p>
    Text1<br>
    <span style="color:blue">span</span><br>
    <a href="http://facebook.com">Facebook</a>
</p>
    <p>
    Text1<br>
    <span style="color:red">span</span><br>
    <a href="http://myspace.com">myspace</a>
</p>

Here's output:

enter image description here

I tried to use document.get.Element.ByTagName, but I don't really know how to use it for elements which are following only after red "span".

Maybe, I should use jQuery?

Upvotes: 0

Views: 105

Answers (1)

David Thomas
David Thomas

Reputation: 253396

If you were to use classes, instead of in-line CSS, you could just CSS:

.redStyle = {
    color: red;
}

.blueStyle {
    color: blue;
}

// style the elements:

.redStyle + br + a, /* multiple adjacent-sibling combinators are required
                       if you insist on retaining the 'br' elements */
.redStyle ~ a {
    color: green;
}

This would, of course, require HTML such as:

<p>
    Text1<br>
    <span class="blueStyle">span</span><br>
    <a href="http://google.com">Google</a>
</p>
<p>
    Text1<br>
    <span class="redStyle">span</span><br>
    <a href="http://bing.com">Bing</a>
</p>

To get the actual nodes, or a property of those nodes, with JavaScript:

function follows(target, cName) {
    while (target.previousSibling) {
        if (target.previousSibling.className && target.previousSibling.className.indexOf(cName) > -1) {
            return true;
        } else {
            target = target.previousSibling;
        }
    }
    return false;
}

var links = document.getElementsByTagName('a'),
    relevantLinks = [],
    relevantText = [];

for (var i = 0, len = links.length; i < len; i++) {
    if (follows(links[i], 'redStyle')) {
        // this adds to the store of relevant links in which you're interested
        relevantLinks.push(links[i]);
        // you could act on them directly, but storing them
        // allows for further use at a later time (if required)
    }
}
for (var i = 0, len = relevantLinks.length; i < len; i++) {
    // iterating over the relevant links/elements, pushing their text
    // into another array to store that text
    relevantText.push(relevantLinks[i]['textContent' || 'innerText']);
}    

console.log(relevantText);

JS Fiddle demo.

Of course, in most modern browsers, if you do use the class-names (again, instead of in-line CSS) you could simply use document.querySelectorAll() to retrieve a nodeList of relevant elements, and iterate over that directly:

var links = document.getElementsByTagName('a'),
    relevantLinks = document.querySelectorAll('.redStyle ~ a'),
    relevantText = [];

for (var i = 0, len = relevantLinks.length; i < len; i++) {
    relevantText.push(relevantLinks[i]['textContent' || 'innerText']);
}

console.log(relevantText);

JS Fiddle demo.

Upvotes: 1

Related Questions