Alnedru
Alnedru

Reputation: 2655

Loop through all images in table with jQuery

I'm not that good in jQuery, or not good at all, but I need to make a jQuery/Javascript function which will search for all images in a particular table. So the criterias are that the table must have an attribute 'summary' and it must be equal to 'forum' (summary=forum)

<table width="100%" class="ms-disc"  dir="none" border="0"
 cellSpacing="0" cellPadding="1" summary="Forum">

So if this is the table then I need to check if td has class "particular class" <td class="ms-disc-bordered">. It is possible that in that td can be another table and only in that table there is a td ... But this is not important, just to know it is nested and can be on lower lvl's that image.

Then the image inside can be resized.

Now I have this code:

function ResizeImages()
{
    jQuery(document).ready(function () 
    {
        var table = $("").find()
        table.each("td")(function()
        {
            if(hassummary & summary.equals("forum"))
            {
                var img=table.find("image")
                img.height="";
                img.weight="";
            }
        }
    }
}

UPDATE: so this is hierarchy:

<table summary="forum">
 .....
    <table>

        <table>
            <tr>
                <td class="particular class">
                    <a link>
                        <image> the one i need to get</image>
                    </a>
                </td>
            </tr>
        </table> </table></table> ....

Upvotes: 0

Views: 3433

Answers (2)

jerjer
jerjer

Reputation: 8766

This should do it:

$('table[summary=forum] td.particular.class img').each(function(){
   $(this).width(w).height(h);
});

Upvotes: 4

frm
frm

Reputation: 3496

The jQuery code to search your images is:

$('table[summary="forum"] td.particular.class img').attr({
    height: '',
    width: ''
});

Despite the utility of this code, I think the most important part for you is to understand the jQuery selector. The selector is composed of this sub-selectors:

  • table[summary="forum"] search all <table> elements with an attribute named summary equals to "forum".
  • td.particular.class search all <td> elements having both the classes particular and class
  • img search all <img> elements.

The sub-selectors are separated by spaces. This means that <img> elements must be placed inside <td> elements and these must be placed inside <table> elements. Other details are in the full documentations for jQuery selectors.

Once you find the correct <img> elements, you can set their attributes using the attr() function.

Upvotes: 1

Related Questions