I am trying to count the number of li elements on a remote page, called file.xml. What am I doing wrong? Sorry for the easy question but I just can’t figure this out.
Javascript:
<script>
$(document).ready(function(){
$.get('/file.xml', function(data)
{
var allslides = $(data).find('li'),
var slidenumber = $allslides.length;
alert(slidenumber);
});
});
</script>
HTML (contents of file.xml):
<body>
<li><h2>Headline1</h2><span class="desctext">Description</span></li>
<li><h2>Headline2</h2><span class="desctext">Description</span></li>
<li><h2>Headline3</h2><span class="desctext">Description</span></li>
</body>
Try to specify that you are loading an XML file:
Also I noticed that your variable names don’t really match-up, first you use
allslidesthen you use$allslides. I normalized those variable names in my example code.It also kind of looks like you’re returning HTML rather than XML. Which will work, just change
dataType : 'xml'todataType : 'html'(or leave the option blank ashtmlis the default). In a general sense it’s a good idea to specify a content-type rather than letting jQuery guess on its own.Documentation for
$.ajax(): http://api.jquery.com/jquery.ajax/