tags:

views:

26

answers:

1

Hi,

I have the following HTML (a snippet):

                    <td class="ms-vb-title" height="100%">
                      <table class="ms-unselectedtitle" onmouseover="OnItem(this)"
                      ctxname="ctx1" id="5" url=
                      "/Business%20Divisions/5_.000" dref=
                      "/Business Divisions" perm="0x7fffffffffffffff"
                      type="" ext="" icon="icgen.gif||" otype="0" couid="" hcd=""
                      csrc="" ms="0" ctype="Item" cid=
                      "0x010072628A3517FD5F4A8D399BADE8A6D104" uis="512" surl="">
                        <tbody>
                          <tr>
                            <td class="ms-vb" width="100%"><a onfocus="OnLink(this)"
                            href=
                            "/Business%20Divisions/DispForm.aspx?ID=5"
                            onclick="GoToLink(this);return false;" target=
                            "_self">Commercial</a></td>

                          </tr>
                        </tbody>
                      </table>
                    </td>
                  </tr>

Now I want to get the ID of tables with class .ms-unselectedtitle and store them in an array. I do this with:

var businessUnitID = new Array();
$('.ms-unselectedtitle').each(function(){
businessUnitID.push($(this).attr('id'));

the class .ms-unselectedtitle is used in a lot of other places in the html though so I wonder if there's a way to get all IDs of tables with class .ms-unselectedtitle and that has a parent td with class .ms-vb-title? Or get all ID's of a td, with class .ms-vb-title, table (with class ..ms-unselectedtitle) children?

Thanks in advance.

+1  A: 

You can try this, to get all IDs of tables with class .ms-unselectedtitle(and parent TD with class .ms-vb-title):

// filter out all tables with immediate parent td.ms-vb-title
// returns a collection
var ids = $('.ms-unselectedtitle').filter(function() {
    return $(this).parent('td').hasClass('.ms-vb-title');

// from resulting collection, grab all IDs
}).map(function() {
    return this.id;

// convert to array
}).get();

Try it here: http://jsfiddle.net/Nt7zv/

karim79
ok, how do I chuck this into an array?
Peter
businessUnitID.push(ids); I guess
Peter
Sorry, was adding in some explanation. In the above example, `ids` will be an array with all the IDs of matching elements.
karim79
it works and alsovar businessUnitID2 = new Array(); $('td.ms-vb-title > table.ms-unselectedtitle').each(function(){ businessUnitID2.push($(this).attr('id'));However, I saw that there's another table with the same class inside a td with the same class at another place in the document. That table has an attribute CType though but not the one in the example above. Can I use the code above and add so that it only return ID's from table without a CType attribute?
Peter
@Peter — Try jQuery's attribute selectors: `$("td.ms-vb-title table:not([CType]).ms-unselectedtitle")`
Ben Blank
Ben you mean something like: $('td.ms-vb-title > table:not([CType]).ms-unselectedtitle').each(function(){ businessUnitID2.push($(this).attr('id')); ?
Peter
just saw now that the example does include a ctype, howver, the ctype is always Item for this table, the other table has some other ctypes depending on the table name. So once again I need to alter my question (sorry). How do I get the tables with the class above and has CType Item? Thanks heaps.
Peter
$('td.ms-vb-title > table[CType=Item].ms-unselectedtitle').each(function(){
Peter