May 14, 2012

jquery ajax cant loop thru the found data

Question by user759235

I want to loop thru a file which is loaded with ajax, but it won’t loop, i have tried a couple of things but I cant get it to work.

// jquery

$.ajax({
    url: 'file.html',
    type: "GET",
    dataType: "html",
    success: function(data) {

        $(data).find('div').each(function(i){
            alert('found')
        });

    },
    error: function(){
        alert('oeps...')
    }                           
});

// file.html

<div>
// content goes here
</div>

<div>
// content goes here
</div>

<div>
// content goes here
</div>

...


...    

Answer by Rocket Hazmat

You need to change .find to .filter. This is because .find searches the children descendants of all the elements, but since your html file is just <div>s, you need to use .filter to find them.

DEMO: http://jsfiddle.net/zuPVp/

Answer by Starx

You dont need to specify html as the datatype, it is not needed.

SO, remove the following line.

dataType: "html"
April 4, 2012

jQuery each number

Question by Ivan C.

I have a idea to make table like this

 1. something1
 2. something2
 3. something3

I have made a table:

<table>
   <tr><td id="rb">...
   <tr><td id="rb">...
</table>

so in #rb is this sequence number starting from 1.

I have this jQuery code but it doesn’t work. Can you pls help 🙂

            $('tr').each(function(index) 
            {
                  $('#rb').append(index);
             });

It just make

       012345

in the first #rb

Thanks in advance!

Answer by Starx

You should not give same id to more than one element within same HTML document. You can do the same using class="rb" instead.

$('tr').each(function(index) {
   $(this).find('.rb').append(index);
});
...

Please fill the form - I will response as fast as I can!