views:

39

answers:

3

I'd like to select elements with attribute names (keys) that begin with a set string. Use case: the jQuery UI dialog creates buttons whose only unique identifers are a pseudo-random custom attribute with a sequential value such as jQuery1288273859637="40"

I'd like to select based on the fact that the attribute's name is jQuery* (begins with jQuery)

A: 

Get an reference to the dialog, and then use a css selector to find the button. firebug (or google chrome developer tools) are useful for determining what to look for.

Ben Taitelbaum
This won't work - the OP is looking for the attribute *name* to begin with jQuery - I made the same mistake
Steve Greatrex
you're correct, Steve. What does OP mean/stand for?
Jake
ah right, editing my response to reflect this.
Ben Taitelbaum
@Jake - OP stands for "Original Poster".
patrick dw
+2  A: 

Only think I could think of would be to test the individual keys of every DOM element in a .filter().

This would be horribly inefficient, but if you really wanted it, it could look something like this:

$('body *').filter(function() {
    for( var k in this ) {
        if( this.hasOwnProperty(k) && k.indexOf( "jQuery" ) === 0 ) {
            return true;
        }
    }
    return false;
});

This will loop over all elements in the <body>, and then loop over the key/value properties of each, testing each key to see if it starts with "jQuery". If so, it returns true, and the loop is broken. If not, it returns false after all properties have been tested.

I'd find another way. Seriously, don't do this.

patrick dw
If you know that the elements will appear under a given parent (as with the jQuery dialog) you could improve on the performance by passing in a context. Still nasty though :)
Steve Greatrex
@Steve - You're right, I ignored that the question was talking about UI dialogs. Limiting the elements would help, but like you said, still nasty.
patrick dw
this would be the preferred method if I didn't attack the problem from a different angle in the end (see checked answer) I'd narrow the context of the query to contain its size, though
Jake
A: 

The attribute you are looking at is the jQuery expando ... it would be completely useless to target these attributes as the number after the "jQuery" is based on the current time and changes with each page reload (ref1 & ref2).

It would be best to target a class in your base HTML. For example, the jQuery UI Dialog should have this base HTML (ref):

<div class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-draggable ui-resizable">
   <div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix">
      <span id="ui-dialog-title-dialog" class="ui-dialog-title">Dialog title</span>
      <a class="ui-dialog-titlebar-close ui-corner-all" href="#"><span class="ui-icon ui-icon-closethick">close</span></a>
   </div>
   <div style="height: 200px; min-height: 109px; width: auto;" class="ui-dialog-content ui-widget-content" id="dialog">
      <p>Dialog content goes here.</p>
   </div>
</div>

Find the id or class you want to target and use it instead!

If you need to target a button in the dialog, then use something like this:

$('.ui-dialog .ui-button:contains("Cancel")')
fudgey
fudgey - Not *completely useless* since OP wanted to target those that *begin with jQuery*, but certainly selecting by class would be *much* better than filtering property names.
patrick dw
I ultimately went with a solution similar to this:$(":contains('Transfer')", $(".ui-button", ".ui-dialog"))[0].parentElement but I'll switch to your code instead
Jake
@patrick dw: you're right I guess my "completely useless" statement came out a bit strong. I just meant to say there are much better ways to do this, thanks for the feedback :)
fudgey