tronen wrote:
Hi, I am new to jQuery and is curious if this code can be shortened
down? It feels like it can, but I couldn't find any way to use jQuery
selectors if I have a specific reference to a object.

jquery(myElement) returns a jQuery object around that element, much the same way that jquery("selector") returns a jQuery object around the elements matching the selector.


What I want to do is to find the element to the parent of "el" that
the id starts with linecolorbox. I know how to do it if I want to find
ALL the ones that id start with linecolorbox,  but not if I want to
start with a specific element as reference (el.parentNode).

Looking at the code you supplied, you seem to be searching for children and/or what might be called nieces/nephews of your node with ids starting with "linecolorbox". But your description above sounds more like you want any descendants of that parent node with the right ids. The latter is probably easier:

    $(myElement).parent().find("[id^=linecolorbox]").each(function() {
         // do something here.
    });

If you do just want the grandchildren of your element's parent, you might try {

    $(myElement).parent().find("> * > * [id^=linecolorbox]")
        .each(function() {
            // do something here.
        });

or some such.  So your function might be rewritten like:

    jQ('.linecolor').each(function() {
        this.value = (this.value.match(/^f{6}$/i)) ? '' : this.value;
        var match = el.value.match(/^[0-9a-f]{6}$/i);
        jQ(this).parent().find("[id^=linecolorbox]").each(function() {
            this.innerHTML = match
                           ? '    '
                           : ' X ';
        });
    });

or with the other selector if you really want the nieces/nephews only.

Of course all this is untested...

Cheers,

  -- Scott

Reply via email to