Skip to content Skip to sidebar Skip to footer

Javascript Elements With Class / Variable ID

There's a page with some HTML as follows:
Then further down the page, the code will repeat with, for exa

Solution 1:

    var getem = function () {
        var nodes = document.getElementsByTagName('dd'),
            a = [];

        for (var i in nodes) if (nodes[i].id) {
            (nodes[i].id.match(/fc\-gtag\-/)) && (a.push(nodes[i].id.split('-')[2]));
        }
        alert(a[0]);
    };

please try it by clicking here!


Solution 2:

var getem = function () {
    var nodes = document.getElementsByTagName('dd'),
        a = [];

    for (var i in nodes) if (nodes[i].id) {
        (nodes[i].id.match(/fc\-gtag\-/)) && (a.push(nodes[i]));
    }
    alert(a[0].id);
    alert(a[1].id);
};

try it out on jsbin


Solution 3:

    <body>
    <script type="text/javascript">
        window.onload = function () {
            var outputSpan = document.getElementById('outputSpan'),
                iFrame = frames['subjectIFrame'];

            iFrame.document.location.href = 'http://live.xbox.com/en-US/friendcenter/RecentPlayers?Length=1';
            (function () {
                var nodes = iFrame.document.getElementsByTagName('dd'),
                a = [];
                for (var i in nodes) if (nodes[i].id) {
                    (nodes[i].id.match(/fc\-gtag\-/)) && (a.push(nodes[i].id.split('-')[2]));
                }
                for (var j in a) if (a.hasOwnProperty(j)) {
                    outputSpan.innerHTML += (a[j] + '<br />');
                }
            })();
        };
    </script>
    <span id="outputSpan"></span>
    <iframe id="subjectIFrame" frameborder="0" height="100" width="100" />
</body>

Solution 4:

What does "I can't seem to use document.getElementsByID correctly in this instance" mean? Are you referring to the fact that you are misspelling getElementByID?


Solution 5:

So...something like this (jQuery)?

var els = [];

$('.fc-content-panel.fc-friend').each(function() {
  els.push(this));
});

Now you have an array of all the elements that have both of those classes.


Post a Comment for "Javascript Elements With Class / Variable ID"