From: williamc on
On 8/12/2010 5:38 AM, SAM wrote:
> Le 11/08/10 12:49, williamc a �crit :
>> On 8/11/2010 5:54 AM, Gregor Kofler wrote:
>>> Am 2010-08-11 05:30, schrieb Denis McMahon:
>>>> On 10/08/10 21:57, Evertjan. wrote:
>>>>> Denis McMahon wrote on 10 aug 2010 in comp.lang.javascript:
>>>>>
>>>>>> At the moment I use named elements, and group the elements by giving
>>>>>> them a common name, but this feels "wrong". For example:
>>>>>>
>>>>>> function vis(name, state)
>>>>>> {
>>>>>> var els = document.getElementsByName(name);
>>>>>> for (var el in els) els[el].style.visible = state;
>>>>>> }
>>>
>>> This rather works by coincidence, since el will become any property of
>>> your els collection
> (...)
>>> A working approach:
>>>
>>> var l = els.length;
>>> while(l--) {
>>> els[l].style.visibility = state;
>>> }
>>>
>>>>>>
>>>>>> Has anyone got a "better" way to do this sort of thing?
> (...)
>>> (untested)
>>> function gEBCN(cN) {
>>> var els = document.getElementsByTagName("*");
>>> var result = [];
>>> var l = els.length;
>>>
>>> while(l--) {
>>> if(els[l].className == cN) {
>>> results.push(els[l]);
>>> }
>>> }
>>> return result;
>>> }
>>>
>>> The main difference: the "native" gEBCN() will return a live collection,
>>> the custom version a snapshot.
>>
>> Good point about the difference between the native implementation
>> returning a (live) node list vs. just a snapshot.
>>
>> Any solution must work with multiple values, though, e.g. class="foo
>> bar" and I don't see how the function above will do that.
>
> function gEBCN(cN) {
> if(document.getElementsByClassName)
> return document.getElementsByClassName(cN);
> var els = document.getElementsByTagName("*"),
> l = els.length,
> result = [];
> while(l--)
> if(els[l].className && els[l].className.indexOf(cN) >= 0)
> results.push(els[l]);
> return result;
> }
>
>
> I do not understand all other complications ... :-(
> (as they seem to me very useless)
>

Then you probably won't like this one... :)

http://robertnyman.com/2008/05/27/the-ultimate-getelementsbyclassname-anno-2008/


--

--williamc
From: SAM on
Le 12/08/10 11:54, Gregor Kofler a �crit :
> Am 2010-08-12 11:38, SAM meinte:
>
>> function gEBCN(cN) {
>> if(document.getElementsByClassName)
>> return document.getElementsByClassName(cN);
>> var els = document.getElementsByTagName("*"),
>> l = els.length,
>> result = [];
>> while(l--)
>> if(els[l].className && els[l].className.indexOf(cN) >= 0)
>> results.push(els[l]);
>> return result;
>> }
>>
>>
>> I do not understand all other complications ... :-(
>> (as they seem to me very useless)
>
> Your solution has at least two problems:
> * Using the native method will return a live collection

I do not see what is the problem with returning a collection
(why to speak of 'life' ?
is there a difference with any other DOM collection ?)

> (elements will join or drop out automatically, when they get the
> appropriate class assigned).

Found something via Google trying to explain what could cover this term
of 'life' ...
I would then say :
the problem is more the alternative return in a fixed array (of elmts)

> * Your indexOf() will find a class "red"

what a strange idea ... a red class ...

> even when elements have a class "redAndBlue"

and stronger again ! ;-)

> - you will need a RegExp.

we will need useful rules and name of rules not to much unreadable

OK for the regexp (usually not with my poor css that never has a rule's
name built with other names)

Finally, if the goal is to get an array of elements at a time t,

function gEBCN(cN) {
var result = [], els, l, r = new RegExp('(^| )'+cN+'($| )');
if(document.getElementsByClassName)
els = document.getElementsByClassName(cN);
else if(document.getElementsByTagName)
els = document.getElementsByTagName("*");
l = els.length;
while(l--)
if(els[l].className && els[l].className.match(r) )
result.push(els[l]);
return result;
}

--
St�phane Moriaux avec/with iMac-intel
From: Garrett Smith on
On 2010-08-10 11:55 AM, Denis McMahon wrote:
> At the moment I use named elements, and group the elements by giving
> them a common name, but this feels "wrong". For example:
[...]

> Has anyone got a "better" way to do this sort of thing?
>
I like to add a class to a common ancestor for this sort of thing and
let the browser apply the cascade. An example of that:
<http://jibbering.com/faq/notes/code-guidelines/descendant-sel.html>

You could do the same using attribute selector for the NAME attribute
however support for attribute selectors is not as broad as as support
for class selectors. The class selector works in more browsers.

The class attribute can apply to any element, as was mentioned already,
whereas the NAME attribute applies to different elements.

Also, with the attribute selectors are supported, there are
discrepancies between browsers applyt case sensitive matching to the
attribute value. All of that can be explained by the specification and
with examples to show which browsers do what but if you don't use
attribute selectors, it's irrelevant anyway.
--
Garrett
From: SAM on
Le 12/08/10 15:27, williamc a �crit :
>
> I'm not the OP. I just jumped in and asked what people here use for
> gEBCN, while noting that Gregor's example for the OP would need some
> additional code to cover multiple values.
>
> I very frequently use multiple classes, and in general the person doing
> the scripting might not have control over the HTML/CSS. So, I most
> definitely want a gEBCN function that works for "foo bar".

// one class :

function gEBCN(cN) {
var result = [], els, l, r = new RegExp('\\b'+cN+'\\b');
if(document.getElementsByClassName)
els = document.getElementsByClassName(cN);
else if(document.getElementsByTagName)
els = document.getElementsByTagName("*");
l = els.length;
while(l--)
if(els[l].className && els[l].className.match(r) )
result.push(els[l]);
return result;
}

// multi classes :

function gEBXCN() {
var n1 = n2 = arguments.length, g = [], tri = function(a,b){
var c = [];
for(var i in a) { for(var j in b) if(a[i]==b[j]) c.push(a[i]);}
return c;
};
if(n2==1) return gEBCN(arguments[0]);
while(n1--) g[n1] = gEBCN(arguments[n1]);
while(n2-- && n2>0) {
g[n2] = tri(g[n2], g[n2-1])
}
return g[1];
}

alert(gEBXCN('bar', 'foo', 'fruit').length);



--
St�phane Moriaux avec/with iMac-intel
From: williamc on
On 8/11/2010 8:40 AM, Gregor Kofler wrote:
> Am 2010-08-11 12:49, williamc meinte:

....

>> ... I'd be
>> interested to see what people use in their own work.
>
> element.gEBCN() (transfered into a "static" array), document.evaluate(),
> filtered element.gEBTN().
>
> Gregor
>

OK. I read your code in vxJS core. Gotta try those widgets, too. Nice!

--

-williamc
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5
Prev: FYI: Douglas Crockford: Loopage
Next: Getting there....