[Proto-Scripty] Re: testing whether all elements in a div have a certain class
That's broadly right, but my problem is string concatenation. I'm passing the div name to search for those spans in using a function, so when I try to do: var spanName = span_+levelID; I just get a value of 'span_+levelID' for $spanName. Matt On Nov 7, 4:05 pm, Alex Mcauley [EMAIL PROTECTED] wrote: okay i am trying to get this correct .. You want to see if a certain amount of spans in an element have a class name of 'complete' and if so do something else firstly you can easily count the amount with $$ var $complete=$$('.complete').length then you want to do something if they are more than or equal to 3 if(($complete) ($complete=3) { do_something(); } is this correct so far .. Sorry i am having trouble deciphering what you want to achieve Alex - Original Message - From: Matt Andrews [EMAIL PROTECTED] To: Prototype script.aculo.us prototype-scriptaculous@googlegroups.com Sent: Friday, November 07, 2008 3:41 PM Subject: [Proto-Scripty] Re: testing whether all elements in a div have a certain class Can anyone help? I'm really close to solving this, I just need someone with a better grasp of Javascript to prod me in the right direction... On Nov 3, 9:51 am, Matt [EMAIL PROTECTED] wrote: On Nov 1, 4:38 pm, Jarkko Laine [EMAIL PROTECTED] wrote: 1) your alerting with a string bar there. 2) $$ returns an array of elements. If you want a single item, why not give the span a unique id and call it with $? If you need to use the double-dollar selector, you need to get the first element of the array to get the actual element: $$('#container_'+boxID+' span')[0]; One option that gives you the span element (with the html syntax you posted before) is this: $('container_' + boxID).down('span'). a href=# onclick=foo(24);click here/a // returns $$('#container_24 span'); You mean the function above alerts that string? Frankly, I don't believe you :-) The parameter that $ and $$ take is just a normal string. There's nothing magical about it. So if you can do string + someVar, and the result is a string, you can give it as a parameter to those functions. //jarkko I tried out those methods (the [0] ending and the .down function), neither worked. I'm still struggling to get my head around this - I can only pass the ID of the element through a function, so it's always going to be a variable, I can't know it in advance. Here's some actual code (the above stuff was a simplified (and badly-written...) function, not the actual one): //contained within my function: var spanName = span_+levelID; //levelID is passed in the function var spanArray = document.getElementsByClassName(name); // gets all the elements I want to test var completeArray = spanArray.getElementsByClassName('complete'); // should get all elements within the above array with a class of 'complete' // if the number of spans is the same as the number of spans with 'complete' class: if(spanArray.size() == completeArray.size()) { $('level2_click_1').removeClassName('incomplete'); $('level2_click_1').addClassName('complete'); $('level2_click_1').update(ON); } else { $('level2_click_1').removeClassName('complete'); $('level2_click_1').addClassName('incomplete'); $('level2_click_1').update(OFF); } HTML: span class=span_1 incomplete id=level3_click_1 a href=javascript:// onclick=changeState('level3', '1', 'on')OFF/a /span span class=span_2 incomplete id=level3_click_2 a href=javascript:// onclick=changeState('level3', '2', 'on')OFF/a /span span class=span_3 incomplete id=level3_click_3 a href=javascript:// onclick=changeState('level3', '3', 'on')OFF/a /span Basically, when all three of those spans have class name 'complete' (eg, are turned on) I want a different span somewhere else (the 'level2_click_1' span, which I will need to turn into a variable rather than a hardcoded reference) needs to switch to 'complete' too. Does this make any more sense now? Matt --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: testing whether all elements in a div have a certain class
Can anyone help? I'm really close to solving this, I just need someone with a better grasp of Javascript to prod me in the right direction... On Nov 3, 9:51 am, Matt [EMAIL PROTECTED] wrote: On Nov 1, 4:38 pm, Jarkko Laine [EMAIL PROTECTED] wrote: 1) your alerting with a string bar there. 2) $$ returns an array of elements. If you want a single item, why not give the span a unique id and call it with $? If you need to use the double-dollar selector, you need to get the first element of the array to get the actual element: $$('#container_'+boxID+' span')[0]; One option that gives you the span element (with the html syntax you posted before) is this: $('container_' + boxID).down('span'). a href=# onclick=foo(24);click here/a // returns $$('#container_24 span'); You mean the function above alerts that string? Frankly, I don't believe you :-) The parameter that $ and $$ take is just a normal string. There's nothing magical about it. So if you can do string + someVar, and the result is a string, you can give it as a parameter to those functions. //jarkko I tried out those methods (the [0] ending and the .down function), neither worked. I'm still struggling to get my head around this - I can only pass the ID of the element through a function, so it's always going to be a variable, I can't know it in advance. Here's some actual code (the above stuff was a simplified (and badly-written...) function, not the actual one): //contained within my function: var spanName = span_+levelID; //levelID is passed in the function var spanArray = document.getElementsByClassName(name); // gets all the elements I want to test var completeArray = spanArray.getElementsByClassName('complete'); // should get all elements within the above array with a class of 'complete' // if the number of spans is the same as the number of spans with 'complete' class: if(spanArray.size() == completeArray.size()) { $('level2_click_1').removeClassName('incomplete'); $('level2_click_1').addClassName('complete'); $('level2_click_1').update(ON); } else { $('level2_click_1').removeClassName('complete'); $('level2_click_1').addClassName('incomplete'); $('level2_click_1').update(OFF); } HTML: span class=span_1 incomplete id=level3_click_1 a href=javascript:// onclick=changeState('level3', '1', 'on')OFF/a /span span class=span_2 incomplete id=level3_click_2 a href=javascript:// onclick=changeState('level3', '2', 'on')OFF/a /span span class=span_3 incomplete id=level3_click_3 a href=javascript:// onclick=changeState('level3', '3', 'on')OFF/a /span Basically, when all three of those spans have class name 'complete' (eg, are turned on) I want a different span somewhere else (the 'level2_click_1' span, which I will need to turn into a variable rather than a hardcoded reference) needs to switch to 'complete' too. Does this make any more sense now? Matt --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: testing whether all elements in a div have a certain class
okay i am trying to get this correct .. You want to see if a certain amount of spans in an element have a class name of 'complete' and if so do something else firstly you can easily count the amount with $$ var $complete=$$('.complete').length then you want to do something if they are more than or equal to 3 if(($complete) ($complete=3) { do_something(); } is this correct so far .. Sorry i am having trouble deciphering what you want to achieve Alex - Original Message - From: Matt Andrews [EMAIL PROTECTED] To: Prototype script.aculo.us prototype-scriptaculous@googlegroups.com Sent: Friday, November 07, 2008 3:41 PM Subject: [Proto-Scripty] Re: testing whether all elements in a div have a certain class Can anyone help? I'm really close to solving this, I just need someone with a better grasp of Javascript to prod me in the right direction... On Nov 3, 9:51 am, Matt [EMAIL PROTECTED] wrote: On Nov 1, 4:38 pm, Jarkko Laine [EMAIL PROTECTED] wrote: 1) your alerting with a string bar there. 2) $$ returns an array of elements. If you want a single item, why not give the span a unique id and call it with $? If you need to use the double-dollar selector, you need to get the first element of the array to get the actual element: $$('#container_'+boxID+' span')[0]; One option that gives you the span element (with the html syntax you posted before) is this: $('container_' + boxID).down('span'). a href=# onclick=foo(24);click here/a // returns $$('#container_24 span'); You mean the function above alerts that string? Frankly, I don't believe you :-) The parameter that $ and $$ take is just a normal string. There's nothing magical about it. So if you can do string + someVar, and the result is a string, you can give it as a parameter to those functions. //jarkko I tried out those methods (the [0] ending and the .down function), neither worked. I'm still struggling to get my head around this - I can only pass the ID of the element through a function, so it's always going to be a variable, I can't know it in advance. Here's some actual code (the above stuff was a simplified (and badly-written...) function, not the actual one): //contained within my function: var spanName = span_+levelID; //levelID is passed in the function var spanArray = document.getElementsByClassName(name); // gets all the elements I want to test var completeArray = spanArray.getElementsByClassName('complete'); // should get all elements within the above array with a class of 'complete' // if the number of spans is the same as the number of spans with 'complete' class: if(spanArray.size() == completeArray.size()) { $('level2_click_1').removeClassName('incomplete'); $('level2_click_1').addClassName('complete'); $('level2_click_1').update(ON); } else { $('level2_click_1').removeClassName('complete'); $('level2_click_1').addClassName('incomplete'); $('level2_click_1').update(OFF); } HTML: span class=span_1 incomplete id=level3_click_1 a href=javascript:// onclick=changeState('level3', '1', 'on')OFF/a /span span class=span_2 incomplete id=level3_click_2 a href=javascript:// onclick=changeState('level3', '2', 'on')OFF/a /span span class=span_3 incomplete id=level3_click_3 a href=javascript:// onclick=changeState('level3', '3', 'on')OFF/a /span Basically, when all three of those spans have class name 'complete' (eg, are turned on) I want a different span somewhere else (the 'level2_click_1' span, which I will need to turn into a variable rather than a hardcoded reference) needs to switch to 'complete' too. Does this make any more sense now? Matt --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: testing whether all elements in a div have a certain class
There is something very confusing about what you have said. In your text you say I can only pass the ID of the element through a function, in the code you refer to span_1 etc as the 'spanName', but then it is actually a class name. Is it really an ID, a name, or a class? You only exhibit a single span with each class name 'span_1' etc. so I'm guessing that you have several spans with class 'span_1', several with 'span_2' etc. Is that the case? If so, what I think you want is if $$('span'+level_ID).all(function(span) {return span.hasClassName('complete');}) { ... On the other hand, if you have only one 'span_1', only one 'span_2', then I don't understand why you are using these separate classnames, as they are effectively duplicating the id's. In this case, I would recommend you give them all a single class (say 'myspan'), and then write if $$('myspan').all(function(span) {return span.hasClassName('complete');}) { ... Colin On Nov 3, 9:51 am, Matt [EMAIL PROTECTED] wrote: On Nov 1, 4:38 pm, Jarkko Laine [EMAIL PROTECTED] wrote: 1) your alerting with a string bar there. 2) $$ returns an array of elements. If you want a single item, why not give the span a unique id and call it with $? If you need to use the double-dollar selector, you need to get the first element of the array to get the actual element: $$('#container_'+boxID+' span')[0]; One option that gives you the span element (with the html syntax you posted before) is this: $('container_' + boxID).down('span'). a href=# onclick=foo(24);click here/a // returns $$('#container_24 span'); You mean the function above alerts that string? Frankly, I don't believe you :-) The parameter that $ and $$ take is just a normal string. There's nothing magical about it. So if you can do string + someVar, and the result is a string, you can give it as a parameter to those functions. //jarkko I tried out those methods (the [0] ending and the .down function), neither worked. I'm still struggling to get my head around this - I can only pass the ID of the element through a function, so it's always going to be a variable, I can't know it in advance. Here's some actual code (the above stuff was a simplified (and badly-written...) function, not the actual one): //contained within my function: var spanName = span_+levelID; //levelID is passed in the function var spanArray = document.getElementsByClassName(name); // gets all the elements I want to test var completeArray = spanArray.getElementsByClassName('complete'); // should get all elements within the above array with a class of 'complete' // if the number of spans is the same as the number of spans with 'complete' class: if(spanArray.size() == completeArray.size()) { $('level2_click_1').removeClassName('incomplete'); $('level2_click_1').addClassName('complete'); $('level2_click_1').update(ON); } else { $('level2_click_1').removeClassName('complete'); $('level2_click_1').addClassName('incomplete'); $('level2_click_1').update(OFF); } HTML: span class=span_1 incomplete id=level3_click_1 a href=javascript:// onclick=changeState('level3', '1', 'on')OFF/a /span span class=span_2 incomplete id=level3_click_2 a href=javascript:// onclick=changeState('level3', '2', 'on')OFF/a /span span class=span_3 incomplete id=level3_click_3 a href=javascript:// onclick=changeState('level3', '3', 'on')OFF/a /span Basically, when all three of those spans have class name 'complete' (eg, are turned on) I want a different span somewhere else (the 'level2_click_1' span, which I will need to turn into a variable rather than a hardcoded reference) needs to switch to 'complete' too. Does this make any more sense now? Matt --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: testing whether all elements in a div have a certain class
On Nov 3, 11:12 am, ColinFine [EMAIL PROTECTED] wrote: There is something very confusing about what you have said. In your text you say I can only pass the ID of the element through a function, in the code you refer to span_1 etc as the 'spanName', but then it is actually a class name. Is it really an ID, a name, or a class? You only exhibit a single span with each class name 'span_1' etc. so I'm guessing that you have several spans with class 'span_1', several with 'span_2' etc. Is that the case? If so, what I think you want is if $$('span'+level_ID).all(function(span) {return span.hasClassName('complete');}) { ... On the other hand, if you have only one 'span_1', only one 'span_2', then I don't understand why you are using these separate classnames, as they are effectively duplicating the id's. In this case, I would recommend you give them all a single class (say 'myspan'), and then write if $$('myspan').all(function(span) {return span.hasClassName('complete');}) { ... Colin Thank you Colin. In the interests of simplicity I'm trying to keep the full code away from this post, it'll only make things more complex. Basically, I'm working on a three-tier menu system. You can enable/ disable a menu option, and if all of the child items of a menu item are enabled, then the parent becomes enabled. The reason I have all the span_1, span_2 etc is because there are about 50 potential child elements. Consider: Option 1 -- Option 2a Option 2b --- Option 3a Option 2c Option 3b Option 2 -- Option 2d --- Option 3c Option 2e Option 3d Option 3e I want to make it so when I enable option 3a and 3b, option 2b changes to 'complete'. This is why those classnames have to be unique - they could easily be IDs instead, but I still have the same issue. Each collection of options is contained within a div whose ID is dynamically generated, so I only want to test for spans within that particular div. Do you follow now? Matt --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: testing whether all elements in a div have a certain class
On Oct 31, 8:20 pm, Jarkko Laine [EMAIL PROTECTED] wrote: Matt, could you elaborate a bit more? $$('#container_'+boxID+' span'); actually looks to be fine. What are you getting with it and what are you expecting? //jarkko I'm getting the literal string, $$('#container_'+boxID+' span'), with the plus symbols and everything. I'm expecting something like so: function foo(boxID) { var bar = $$('#container_'+boxID+' span'); alert(bar); } a href=# onclick=foo(24);click here/a // returns $$('#container_24 span'); Kangax: that method won't work for me, it's pulling stuff out of a database so the id numbers don't increment in any kind of order. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: testing whether all elements in a div have a certain class
On Nov 1, 11:57 am, Matt [EMAIL PROTECTED] wrote: [...] Kangax: that method won't work for me, it's pulling stuff out of a database so the id numbers don't increment in any kind of order. Ok. How do you associate those numbers with certain elements then? You can also iterate over an array of numbers (those from your DB): [1,15,3,42,15].each(function(n) { $$('#container_' + n + ' span'); }); -- kangax --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: testing whether all elements in a div have a certain class
On 31.10.2008, at 21.25, Gabriel Gilini wrote: I'm thinking $$(eval('#container_'+boxID+' span')); Don't know if it's the right thing to do though. Why would you want to eval that string? Matt, could you elaborate a bit more? $$('#container_'+boxID+' span'); actually looks to be fine. What are you getting with it and what are you expecting? //jarkko -- Jarkko Laine http://jlaine.net http://dotherightthing.com http://odesign.fi Check out my latest book, Unobtrusive Prototype, fresh off the Peepcode oven: http://peepcode.com/products/unobtrusive-prototype-js --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: testing whether all elements in a div have a certain class
On Oct 31, 1:21 pm, Matt [EMAIL PROTECTED] wrote: Thanks kangax, that worked well. One question: since I'm working with dynamically-generated code, is there some way I can concatenate variable names? For instance, this code is working in a function, so I actually want to use an ID number to identify #container. I've tried something like: $$('#container_'+boxID+' span'); in order to end up with: $$('#container_1 span'); $$('#container_2 span'); $$('#container_3 span'); etc but this just prints the string out literally. How can I embed the variables in there? I would rather give those elements unique className, but if you must: $R(1,10).each(function(n) { $$('#container_'+ n +' span'); }) should do it. Thanks Matt [snip] -- kangax --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---
[Proto-Scripty] Re: testing whether all elements in a div have a certain class
Indeed, I don't know why I said that lol.I guess I was thinking of var or function name. Gabriel Gilini www.usosim.com.br [EMAIL PROTECTED] [EMAIL PROTECTED] On Fri, Oct 31, 2008 at 6:20 PM, Jarkko Laine [EMAIL PROTECTED] wrote: On 31.10.2008, at 21.25, Gabriel Gilini wrote: I'm thinking $$(eval('#container_'+boxID+' span')); Don't know if it's the right thing to do though. Why would you want to eval that string? Matt, could you elaborate a bit more? $$('#container_'+boxID+' span'); actually looks to be fine. What are you getting with it and what are you expecting? //jarkko -- Jarkko Laine http://jlaine.net http://dotherightthing.com http://odesign.fi Check out my latest book, Unobtrusive Prototype, fresh off the Peepcode oven: http://peepcode.com/products/unobtrusive-prototype-js --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To post to this group, send email to prototype-scriptaculous@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~--~~~~--~~--~--~---