1

I have an html block, that contains a lot of children.

<div id="selectedId">
    <div class=".."><a class=".."></a>
        <button style=".."></button>
    </div>
    <div id="otherId">
        <table></table>
    </div>
</div>

I am using jquery to select the parent. I am then trying to loop over all the descendants and access various properties, such as class or innerHtml. I seem unable to access their properties.

function rewriteCategoryName(oldCategoryName, newCategoryName)
{
    // get div parent
    var parentDiv = $("#selectedId");   

    parentDiv.find('*').each( function(el,key) {
        alert(el+" --");
        el.each( function(el,key) {
           // i get just numbers here, and undefined on everything 
           // i try to access
        });

    });
}

Any ideas? What am i doing wrong here? Is there a concept that i am missing?

Manish Kumar
  • 15,269
  • 5
  • 18
  • 27
Mihai Raulea
  • 428
  • 1
  • 3
  • 18

2 Answers2

1
$("#selectedId").find('*').each( function(el,key) {

    $(this).each(function() {
      $.each(this.attributes, function() {
        // this.attributes is not a plain object, but an array
        // of attribute nodes, which contain both the name and value
        if(this.specified) {
          console.log(this.name, this.value);
        }
      });
    });

});
Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193
1

You've switched the order in the each function, the key is the first parameter and the element is the second, so change:

$("#selectedId").find('*').each( function(el,key)

to:

$("#selectedId").find('*').each( function(key,el) {

for more details: http://api.jquery.com/jQuery.each/

Tomer
  • 17,787
  • 15
  • 78
  • 137