Thursday, October 17, 2013

Javascript get Max Width of element in childnode

this function will return that the max width of element from tag element to inner element 




var mainDiv = document.getElementById('mainDiv');
  contentWidth = document.getElementById('contentScroller').offsetWidth;
   var maxVal = getMax (mainDiv, contentWidth);


function getMax (tag, val){
  if(typeof tag.offsetWidth != "undefined"){
   if(val < tag.offsetWidth){
    val = tag.offsetWidth;
   }
  }
 
  if(tag.children.length>0){
   var i=0;
   for (i=0;i<tag.children.length;i++){
    var childVal = getMax(tag.children[i], val);
    if(val < childVal){
     val = childVal;
    } 
   }
  }
  return val;
 }

No comments:

Post a Comment