1

I have the following code:

var parentEls = $(root)
                .find("credit standard")
                .parents()
                .map(function () { 
                  return this.tagName; 
                    })
                .get()
                .join(" > ");
                alert(parentEls);

            $("#breadcrumb").append("<p>" + parentEls + "</p>");

This works fine to return the parents. However, I don't want to return tagName, I want the name attribute for each parent instead. I tried

.map(function () { 
                      return this.attr("name"); 
                        })

but this doesn't work. Is there any way to do this without resorting to a loop?

4

3 Answers 3

2

attr is no native method, it's a jQuery method. So try to wrap your this into a jQuery constructor:

return $(this).attr('name');

or use the getAttribute method instead:

return this.getAttribute('name');
0
0

You have to wrap the element with jQuery to use .attr. Either use

this.name

or $(this).attr('name')

0

I would try the following

.map(function () { 
   return $(this).attr("name"); 
 })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.