Skip to main content

Bug Tracker

Side navigation

#9638 closed feature (duplicate)

Opened June 21, 2011 06:04PM UTC

Closed June 21, 2011 06:09PM UTC

Last modified June 21, 2011 07:35PM UTC

Suggestion for new Traversal function: orChild()

Reported by: Owned by:
Priority: low Milestone:
Component: traversing Version: 1.6.1
Keywords: Cc:
Blocked by: Blocking:

Today I had the following problem. I had to write a function to update the text of a button. The trouble is the button in question could have either of the following two forms:

a) <button>Text</button>\\\\

b) <button><span>Text</span></button>

I had trouble coming up with an elegant and simple way cater for either variant. A user on the forums suggested:

$("button").children().andSelf().not(":has(*)").text("New Label");

which works, but is maybe not elegant or fast.

I was thinking that perhaps a new traversal function might be useful for situations like this. My suggestion is:

$("button").orChild("span").text("New Label");

For each element in the jQuery object, orChild would select the children that match the given selection string (if it exists). If no matching child is found, the existing element (ie parent) is kept.

If no parameter was supplied to orChild() then all children would be selected (if they exist).

So for

would select three elements (the three <li>s).\\\\
would also select three objects (<li>, <li><b>, <li><i>)\\\\
would select three objects (<li>, <li><b>, <li>)
could be used to update the text of all three <li>s without affecting the <b> or <li>, giving:

Perhaps as an alternative to orChild(), a more general orDescendant() might be a better choice?

Do the devs think this might be a useful addition?

Attachments (0)
Change History (3)

Changed June 21, 2011 06:09PM UTC by rwaldron comment:1

component: unfiledtraversing
priority: undecidedlow
resolution: → duplicate
status: newclosed

This is a duplicate of rejected 1.7 proposal. I wrote a plugin to satisfy the request, which can be found here:

Changed June 21, 2011 06:09PM UTC by rwaldron comment:2

Duplicate of #7194.

Changed June 21, 2011 07:35PM UTC by anonymous comment:3

#7194 is not an exact duplicate of my proposal. The or() function could be used to solve my <button> use case, but not the <li> use case.