Bug Tracker

Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#9638 closed feature (duplicate)

Suggestion for new Traversal function: orChild()

Reported by: [email protected] Owned by:
Priority: low Milestone: 1.next
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


$("li") would select three elements (the three <li>s).
$("li").orChild() would also select three objects (<li>, <li><b>, <li><i>)
$("li").orChild("b") would select three objects (<li>, <li><b>, <li>)

$("li").orChild().text("foo") 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?

Change History (3)

comment:1 Changed 12 years ago by Rick Waldron

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: http://jsfiddle.net/rwaldron/LNC5m/

comment:2 Changed 12 years ago by Rick Waldron

Duplicate of #7194.

comment:3 Changed 12 years ago by anonymous

#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.

Note: See TracTickets for help on using tickets.