[jQuery] Re: Is this the best way?
Assuming that the level starts at 0, I think you could apply successive not filters to the Jquery selection, the first eliminating level=0, the second eliminiating level=1, etc... On Jan 31, 3:46 am, Danny <[EMAIL PROTECTED]> wrote: > jQuery does not have a built-in selector to compare the values of > attributes, but it allows you to write custom filters (analogous to > plugins). I once found documentation on this but can't anymore. You > sort of have to read the source to learn to do this. > You just extend the $.expr[':'] object with your new filter set to a > string that is evaluated, where 'a' is the element being tested and > 'm[3]' is the argument to the filter: > > $.expr[':'].levelGreaterThan = '$.attr(a,"level")>m[3]' ; > > Now you can use this anywhere: > > $('qualif:levelGreaterThan(3)').remove(); > > Far more bizarre but potentially useful would be a custom filter that > tests any attribute: > > $.expr[':'].attr = 'eval(m[3].replace(/^(\\w+)/,"$.attr(a,\\"$1\ > \")"))'; > > and now write: > > $('.qualif:attr(level>3)').remove(); > > Unfortunately the parser for [attr] is not extensible, so you > can't create a [level>3] selector directly > > Danny > On Jan 30, 12:22 pm, Feijó <[EMAIL PROTECTED]> wrote: > > > Hi Cabbite > > > Thanks for your 0.02 > > > Its possible to simple use like this? > > $('.qualif[level>3]').remove(); > > > if level bigger then 3, remove it :) > > > My code is dynamic, I cant just wrote all numbers I dont need to remove, > > like your example. Has to use a condition. > > > Feijó > > > cabbiepete escreveu: > > > > Hi Felix, > > > > I would have thought doing an attribute selector like you suggest was > > > better. > > > > something like > > > > ... $('.qualif[level]).each ... > > > > to at least get rid of anything that doesn't have the level > > > attribute. Also if you use the same tag name for all of these > > > attributes its worth adding that as it also helps with efficiency, > > > i.e. jquery only has to check those tags for level attribute. > > > > Depending on the exact use of the level attribute you might be able > > > to get just the ones you want by using [attribute!=value] selectors. > > > i.e. if you want greater than 4 and start at 0 > > > > $('.qualif[level!=0], .qualif[level!=1], .qualif[level! =2], > > > .qualif[level!=3], qualif[level!=4]').remove(); > > > > Hope that helps. Also if any more expert on jquery knows more am keen > > > to know better also. > > > > Cheers, Pete > > > > On Jan 30, 12:56 pm, Feijó <[EMAIL PROTECTED]> wrote: > > > > Hi, I was just wondering if there is any better way to accomplish > > > > that. Some divs has 'level' attribute, with a number. If that > > > > number is bigger than X, will remove the div. var x=4; // > > > > simulating level=parseFloat($this.attr('level')); > > > > $('.qualif').each(function() { if ($(this).attr('level')>x) > > > > $(this).remove(); }); I don't know if we can set a filter in $('') > > > > to look at a custom attribute, should be simpler than Feijó
[jQuery] Re: Is this the best way?
jQuery does not have a built-in selector to compare the values of attributes, but it allows you to write custom filters (analogous to plugins). I once found documentation on this but can't anymore. You sort of have to read the source to learn to do this. You just extend the $.expr[':'] object with your new filter set to a string that is evaluated, where 'a' is the element being tested and 'm[3]' is the argument to the filter: $.expr[':'].levelGreaterThan = '$.attr(a,"level")>m[3]' ; Now you can use this anywhere: $('qualif:levelGreaterThan(3)').remove(); Far more bizarre but potentially useful would be a custom filter that tests any attribute: $.expr[':'].attr = 'eval(m[3].replace(/^(\\w+)/,"$.attr(a,\\"$1\ \")"))'; and now write: $('.qualif:attr(level>3)').remove(); Unfortunately the parser for [attr] is not extensible, so you can't create a [level>3] selector directly Danny On Jan 30, 12:22 pm, Feijó <[EMAIL PROTECTED]> wrote: > Hi Cabbite > > Thanks for your 0.02 > > Its possible to simple use like this? > $('.qualif[level>3]').remove(); > > if level bigger then 3, remove it :) > > My code is dynamic, I cant just wrote all numbers I dont need to remove, > like your example. Has to use a condition. > > Feijó > > cabbiepete escreveu: > > > Hi Felix, > > > I would have thought doing an attribute selector like you suggest was > > better. > > > something like > > > ... $('.qualif[level]).each ... > > > to at least get rid of anything that doesn't have the level > > attribute. Also if you use the same tag name for all of these > > attributes its worth adding that as it also helps with efficiency, > > i.e. jquery only has to check those tags for level attribute. > > > Depending on the exact use of the level attribute you might be able > > to get just the ones you want by using [attribute!=value] selectors. > > i.e. if you want greater than 4 and start at 0 > > > $('.qualif[level!=0], .qualif[level!=1], .qualif[level! =2], > > .qualif[level!=3], qualif[level!=4]').remove(); > > > Hope that helps. Also if any more expert on jquery knows more am keen > > to know better also. > > > Cheers, Pete > > > On Jan 30, 12:56 pm, Feijó <[EMAIL PROTECTED]> wrote: > > > Hi, I was just wondering if there is any better way to accomplish > > > that. Some divs has 'level' attribute, with a number. If that > > > number is bigger than X, will remove the div. var x=4; // > > > simulating level=parseFloat($this.attr('level')); > > > $('.qualif').each(function() { if ($(this).attr('level')>x) > > > $(this).remove(); }); I don't know if we can set a filter in $('') > > > to look at a custom attribute, should be simpler than Feijó
[jQuery] Re: Is this the best way?
Yeah, its all div and within a particular parent!! I'm alreading having that kind of care, adding specificity for all my selectors. It should improve performance, I bet :) Thanks Joel Joel Birch escreveu: On 31/01/2008, Feijó <[EMAIL PROTECTED]> wrote: Its possible to simple use like this? $('.qualif[level>3]').remove(); I don't think there is a way to do that unfortunately. Pete had a good point about optimising your selector for speed though. For example, if you know all the .qualif elements are divs and are all within a particular parent that has an id, say 'content', then you should write the selector with greater specificity like so: $('#content div.qualif').filter( ... Joel Birch.
[jQuery] Re: Is this the best way?
On 31/01/2008, Feijó <[EMAIL PROTECTED]> wrote: > Its possible to simple use like this? > $('.qualif[level>3]').remove(); I don't think there is a way to do that unfortunately. Pete had a good point about optimising your selector for speed though. For example, if you know all the .qualif elements are divs and are all within a particular parent that has an id, say 'content', then you should write the selector with greater specificity like so: $('#content div.qualif').filter( ... Joel Birch.
[jQuery] Re: Is this the best way?
Hi Cabbite Thanks for your 0.02 Its possible to simple use like this? $('.qualif[level>3]').remove(); if level bigger then 3, remove it :) My code is dynamic, I cant just wrote all numbers I dont need to remove, like your example. Has to use a condition. Feijó cabbiepete escreveu: Hi Felix, I would have thought doing an attribute selector like you suggest was better. something like ... $('.qualif[level]).each ... to at least get rid of anything that doesn't have the level attribute. Also if you use the same tag name for all of these attributes its worth adding that as it also helps with efficiency, i.e. jquery only has to check those tags for level attribute. Depending on the exact use of the level attribute you might be able to get just the ones you want by using [attribute!=value] selectors. i.e. if you want greater than 4 and start at 0 $('.qualif[level!=0], .qualif[level!=1], .qualif[level! =2], .qualif[level!=3], qualif[level!=4]').remove(); Hope that helps. Also if any more expert on jquery knows more am keen to know better also. Cheers, Pete On Jan 30, 12:56 pm, Feijó <[EMAIL PROTECTED]> wrote: > Hi, I was just wondering if there is any better way to accomplish > that. Some divs has 'level' attribute, with a number. If that > number is bigger than X, will remove the div. var x=4; // > simulating level=parseFloat($this.attr('level')); > $('.qualif').each(function() { if ($(this).attr('level')>x) > $(this).remove(); }); I don't know if we can set a filter in $('') > to look at a custom attribute, should be simpler than Feijó
[jQuery] Re: Is this the best way?
Hi Felix, I would have thought doing an attribute selector like you suggest was better. something like ... $('.qualif[level]).each ... to at least get rid of anything that doesn't have the level attribute. Also if you use the same tag name for all of these attributes its worth adding that as it also helps with efficiency, i.e. jquery only has to check those tags for level attribute. Depending on the exact use of the level attribute you might be able to get just the ones you want by using [attribute!=value] selectors. i.e. if you want greater than 4 and start at 0 $('.qualif[level!=0], .qualif[level!=1], .qualif[level! =2], .qualif[level!=3], qualif[level!=4]').remove(); Hope that helps. Also if any more expert on jquery knows more am keen to know better also. Cheers, Pete On Jan 30, 12:56 pm, Feijó <[EMAIL PROTECTED]> wrote: > Hi, > I was just wondering if there is any better way to accomplish that. > Some divs has 'level' attribute, with a number. If that number is bigger > than X, will remove the div. > var x=4; // simulating > level=parseFloat($this.attr('level')); > $('.qualif').each(function() { > if ($(this).attr('level')>x) > $(this).remove(); > }); > I don't know if we can set a filter in $('') to look at a custom attribute, > should be simpler than > Feijó
[jQuery] Re: Is this the best way?
Thats it! Nice code Thanks Joel Feijó Joel Birch escreveu: Hi Feijó, Not sure if I understand, but I'll have a go at it: var x = 4; $('.qualif').filter(function() { return parseFloat( $(this).attr('level') ) > x; }).remove(); Joel Birch.
[jQuery] Re: Is this the best way?
Hi Feijó, Not sure if I understand, but I'll have a go at it: var x = 4; $('.qualif').filter(function() { return parseFloat( $(this).attr('level') ) > x; }).remove(); Joel Birch.