Anonymous
Anonymous

Reputation:

How do I set a the attribute of a parent element to be the attribute of a child?

I think this should be a rather simple thing to do, but I'm completely hangin' here! I'm really not sure how much more descriptive I can be here, the title pretty much explains it all. It's a fairly odd situation because my google skills have completely failed me, and I know the solution is probably so simple to implement.

Would anyone care to explain this to me? Thanks.

EDIT:

Okay, never mind, apparently IE (which I'm testing in for compatibility reasons) doesn't do something correctly. I was using the correct method to do it, it just wasn't happening. But low-and-behold, as soon as I open it in Firefox it works.

Lesson I have learned today: Internet Explorer sucks!

Also, much kudos to those who helped... (I don't know if solve is the right word here) me diagnose this issue. Thanks!

Upvotes: 2

Views: 5546

Answers (2)

mtyaka
mtyaka

Reputation: 8848

Step by step:

var $myElement = $('myElementSelector');      // get your element
var attrValue = $myElement.attr('someAttr');  // get the value of element's attribute
var $parent = $myElement.parent();            // get the element's parent element
$parent.attr('someAttr', attrValue);          // set parent's attribute to child's value 

The last three lines could be expressed more succinctly:

$myElement.parent().attr('someAttribute', $myElement.attr('someAttribute'););

Upvotes: 2

peter p
peter p

Reputation: 798

Not tested, but should roughly resemble this one:

var c = $('#child');
c.attr('title',c.parent().attr('title'));

Upvotes: 4

Related Questions