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();

16 Responses to “Element.Methods.visible may not return what you would expect.”

  1. Adam Says:

    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(); })); } );

  2. Felecia Robbins Says:

    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

  3. Miles Ortiz Says:

    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/

  4. Alonzo Langley Says:

    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

  5. Bettina Mathis Says:

    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

  6. Brant Frost Says:

    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

  7. Ella Chan Says:

    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

  8. Eugene Myers Says:

    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

  9. Maryjo Ochoa Says:

    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

  10. Dottie Day Says:

    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

  11. Jan Ford Says:

    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

  12. Karin Puckett Says:

    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

  13. Rudy Perry Says:

    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

  14. Tandy Says:

    只需10元,就能让你的广告遍布各个网站! 支持图片/超链发送!支持分类/地域网站发送!

    10元,就可能换来意想不到的效果,为什么不尝试下呢! QQ:224549200(注明“广告”)

  15. wow gold Says:

    We have been an ebay power seller and paypal confirmed seller of wow gold for years.We also offer cheap wow gold.

  16. wow gold Says:

    Buy wow gold now. Welcome to our website about world of warcraft gold.

Leave a Reply