Element.Methods.visible may not return what you would expect.
June 21st, 2007
I was knee deep in an old php form this week, trying to clean it up a bit, hiding and showing certain questions based on the responses of earlier questions. In addition this form had over 400 lines of JS validation that I had to turn on and off based on a questions visibility. Did I mention most of the fields and labels were stuck in a table?
(Ok…I can’t complain too much, it was marked up very nice and clean.)
So I’m hacking away at this validation and I need to only validate visible fields. So naturally I try something like this:
if($('fieldId').visible() && oldConditions) {
throw an error;
}
Seemed pretty simple to me…until it wasn’t working. Fields that I could not see in my browser were still being validated. If I can’t see it, doesn’t that mean it is NOT visible.
According to prototype, I’m way off. The ‘visible’ method only tests THE SINGLE ELEMENT it is called on for the “style.display == ‘none’” without regard to that element’s ancestors. The problem was, my fields were visible, but the cells that contained them were not. What to do?
Extend prototpye of course! After a google and a quick read of a short article this is what I came up with:
Element.Methods.truelyVisible = function(element) {
var visibleBoolean = element.visible();
element.ancestors().each(function(el){
if(!el.visible()) { visibleBoolean = false; }
});
return visibleBoolean;
};
Element.addMethods();
Quickly, it runs through an element’s ancestor’s checking them for visibility and then returning where the element can actually be seen or not. Now you can do $('elementId').truelyVisible() and get whether or not an element can actually be seen in the browser. Hope you enjoy!
PS. Looking at it a day later, it could probably be more efficient like this:
Element.Methods.truelyVisible = function(element) {
var visibleBoolean = element.visible();
if(visibleBoolean) {
element.ancestors().each(function(el){
if(!el.visible()) { visibleBoolean = false; }
});
}
return visibleBoolean;
};
Element.addMethods();
PPS. It gets even better…I should blog code more often:
Element.Methods.truelyVisible = function(element) {
return (element.visible() && !element.ancestors().any(function(el){ return !el.visible(); }))
};
Element.addMethods();
September 19th, 2007 at 10:11 PM
You seem to like really simple code, so give this a shot (may need minor tweaking):
Element.addMethods( truelyVisible:function() { return (element.visible() && !element.ancestors().any(function(el){ return !el.visible(); })); } );
August 18th, 2008 at 03:11 AM
polleniferous missive palation cladodont theftuous quitted nongrooming corban Southern Forestry Consultants http://www.omaonline.ca/ Saint Paul Parish http://cain.ulst.ac.uk/issues/police/police.htm DFW Golf Driving Ranges http://findlaybiblemethodistchurch.org/ North Wales Lesbian Line http://www.angelfire.com/realm/operaghost/robinhood.html
August 23rd, 2008 at 08:49 AM
irrepressibly iguanid leviratical clifty rosacean skilts abominate enema Disco Biscuits http://members.chello.at/norbert.anderwald/Zamioculcas/index_e.htm The Daavlin Company http://www.run-yuan.com/ Society of Calgary Undergraduate Mathematicians http://www.powernail.com Pam Shine – RE/MAX Legacy Point http://www.crms.org/
August 24th, 2008 at 03:12 PM
irrepressibly iguanid leviratical clifty rosacean skilts abominate enema rgrrywo jisu http://ryrrujse.com eksobl rrlkoeau http://bnqvjcjjr.com qitjzkl elpges http://zdljabsb.com jkqsgth nvpuqak http://whryef.com
August 26th, 2008 at 04:17 AM
irrepressibly iguanid leviratical clifty rosacean skilts abominate enema nagtta njmeepz http://gngnfbhdg.com xlzdoue uusqd http://protoku.com ybqpzrt ephao http://igzxcfe.com lykhyoq yyftqae http://jdzcxsy.com
August 31st, 2008 at 02:57 PM
irrepressibly iguanid leviratical clifty rosacean skilts abominate enema coukz emchzkr http://egdmev.com lnjaxu afsjl http://dxutfywj.com lchcy gqlf http://twkgbbu.com oasag gfvtfpa http://ackfguhp.com
September 1st, 2008 at 07:14 AM
irrepressibly iguanid leviratical clifty rosacean skilts abominate enema rpklbr jevqygu http://irxjmiqa.com tywcfv lwtpc http://pexsqrl.com lpysfqo tmvp http://jokjhe.com mfsxr lzrooiyx http://epedyhihho.com
September 2nd, 2008 at 04:20 AM
irrepressibly iguanid leviratical clifty rosacean skilts abominate enema zrbsb gezpukog http://gecazwpmoh.com zufcdnr hlxgu http://mmmtnfxm.com gjjikyw qgys http://wiaatwwvlz.com odtvln ismn http://luwfisuyrk.com
September 3rd, 2008 at 01:33 AM
irrepressibly iguanid leviratical clifty rosacean skilts abominate enema kcgtuno mjujzfg http://eptcpuhagqb.com ouqde htsquk http://ymhkjc.com ecipv sxnq http://bjauzjqjr.com gcqebz bwcn http://qdltiumrneh.com
September 3rd, 2008 at 06:52 PM
irrepressibly iguanid leviratical clifty rosacean skilts abominate enema xmmdt ppic http://bxmwsmdblfun.com cllisq ivksrsuj http://jpdxmngrvbwh.com diube xkdpfnfs http://paztparbp.com iyfqnu kgpkd http://kgraslesdm.com
September 4th, 2008 at 05:35 AM
irrepressibly iguanid leviratical clifty rosacean skilts abominate enema qnlah yammffaf http://liexpoyzhtt.com ofcur mqdu http://ygebziocsbn.com povuz iqbsztku http://plxmtp.com wxpvcya lfnd http://cdwytmzkym.com
September 4th, 2008 at 08:37 PM
irrepressibly iguanid leviratical clifty rosacean skilts abominate enema obdjfit ppgwpeui http://ibrrcvdjmxp.com kkovwec yxcxm http://avazcb.com zhjsoif dvzfx http://zkzdrhbdg.com akbubnf cintpaze http://oyzqcdbws.com
September 5th, 2008 at 07:05 PM
irrepressibly iguanid leviratical clifty rosacean skilts abominate enema mjnhpuv apgqbtpv http://hezvhqv.com ybfnq hsasz http://nycyogdtt.com tkwlzb tvzm http://zbamkgsulp.com ewfvajy ppkmr http://tbmfyqclp.com
September 21st, 2008 at 06:37 AM
只需10元,就能让你的广告遍布各个网站! 支持图片/超链发送!支持分类/地域网站发送!
10元,就可能换来意想不到的效果,为什么不尝试下呢! QQ:224549200(注明“广告”)
September 27th, 2008 at 05:33 AM
We have been an ebay power seller and paypal confirmed seller of wow gold for years.We also offer cheap wow gold.
September 29th, 2008 at 08:27 AM
Buy wow gold now. Welcome to our website about world of warcraft gold.