Skip to content Skip to sidebar Skip to footer

Getelementsbyclassname And Ajax

Am I missing something with getElementsByClassName() and querySelectorAll()? In Firefox 9 and Chrome 17 and probably all browsers, it seems that both of these functions return an e

Solution 1:

Since you're working with an xml document, the standard DOM methods dealing with attributes don't apply. One option is to use XPath, like so:

data.evaluate("count(//div[@class='findme'])", data.documentElement, null, XPathResult.NUMBER_TYPE, null).numberValue

In your code:

$.post( '/echo/xml/', 
    {xml:'<div id="wrapper"><div class="findme" id="findme1">findme 1</div><div class="findme">findme 2</div></div>'},
    function(data, textStatus, jqXHR) {
        data = jqXHR.responseXML;
        var msg = 'found ' + data.getElementsByTagName('div').length + ' divs in the AJAX response, <br/>' + 
                  'found ' + data.evaluate("count(//div[@class='findme'])", data.documentElement, null, XPathResult.NUMBER_TYPE, null).numberValue + ' findme elements by ClassName<br/>'

        $('#ajax').html(msg);
    }
);

But it may just be easier to convert from XML or use Sizzle. Since you're using jQuery elsewhere, you could just do $(jqXHR.responseXML.documentElement).find('.findme').length.

Post a Comment for "Getelementsbyclassname And Ajax"