Short Tuto
Short Tuto

Reputation: 59

Script working in FireFox and Chrome but not in Internet explorer

<div class="btn-slides-labels">      
  <input class="btn fa-input" type="submit" value="Download">
  <span id="top-label" class="top-label">Downloads : 1546 Times</span>
  <span id="bottom-label" class="bottom-label">1.2MB .zip</span>
</div>

I want to dynamically modify the width of the elements top-labels and bottom-labels depending to the width of the button "btn fa-input".

But the width of these two labels work correctly in FireFox and Chrome except in Internet explorer.

DEMO

var element = document.getElementsByClassName('btn fa-input');
for(var i = 0; i < element.length; i++) {
  var btnWidth = element[i].offsetWidth;
  var btnHeight = element[i].offsetHeight;
  var toplabel = element[i].closest('div').childNodes[3];
  var bottomlabel = element[i].closest('div').childNodes[5];
	toplabel.style.width = btnWidth-20+'px';
  bottomlabel.style.width = btnWidth-20+'px';

}
body {
  margin-top: 100px;
  text-align: center;
}
.btn-slides-labels {
    display: inline-block;
}
.top-label, .bottom-label {
    margin: -50px 0 0 10px;
    background: #222;
    display: block;
    height: 40px;
    text-align: center;
    font: 12px/45px Helvetica, Verdana, sans-serif;
    color: #fff;
    position: absolute;
    z-index: -1;
    box-sizing : border-box;
    
}
.bottom-label { 
    -webkit-border-radius: 10px 10px 0px 0px;
    -moz-border-radius: 10px 10px 0px 0px;
    border-radius: 10px 10px 0px 0px;
     -webkit-transition: margin 0.5s ease;
    -moz-transition: margin 0.5s ease;
    -o-transition: margin 0.5s ease;
    -ms-transition: margin 0.5s ease;
    transition: margin 0.5s ease;
    line-height: 35px;
 }
 .top-label {
    -webkit-border-radius: 0px 0px 10px 10px;
    -moz-border-radius: 0px 0px 10px 10px;
    border-radius: 0px 0px 10px 10px;
     -webkit-transition: margin 0.5s ease;
    -moz-transition: margin 0.5s ease;
    -o-transition: margin 0.5s ease;
    -ms-transition: margin 0.5s ease;
    transition: margin 0.5s ease;
    line-height: 45px;
}

/* HOVER */
.btn-slides-labels:hover .bottom-label { 
    margin: -100px 0 0 10px;
 }
 
.btn-slides-labels:hover .top-label {
    margin: -20px 0 0 10px;
    /*line-height: 50px;*/
}

 .btn {
     text-align: center;
     background: #dd3333;
     color: #f7f7f7;
     font-size: 023px;
     border-radius: 005px;
     padding: 15px 35px;
     box-shadow: 0 -003px rgba(0, 0, 0, 0.14) inset;
     display: inline-block;
     text-transform: uppercase;	
     text-decoration: none;
     margin: 10px 0px;
     transition: all 0.2s linear 0s;
     -moz-transition: all 0.2s linear 0s;
     -webkit-transition: all 0.2s linear 0s;
     -o-transition: all 0.2s linear 0s;
     border-radius: 5px;
     -moz-border-radius: 5px;
     -webkit-border-radius: 5px;
     -o-border-radius: 5px;
     box-shadow: 0 -4px rgba(0, 0, 0, 0.14) inset;
     -moz-box-shadow: 0 -4px rgba(0, 0, 0, 0.14) inset;
     -webkit-box-shadow: 0 -4px rgba(0, 0, 0, 0.14) inset;
     -o-box-shadow: 0 -4px rgba(0, 0, 0, 0.14) inset;
     letter-spacing: 1.5px;
     border: none;
     cursor: pointer;
 }
 
  .btn:hover {
    background: #2ecc71;
  }
<div class="btn-slides-labels">      
      <input class="btn fa-input" type="submit" value="Download">
      <span id="top-label" class="top-label">Downloads : 1546 Times</span>
      <span id="bottom-label" class="bottom-label">1.2MB .zip</span>
    </div>

<div class="btn-slides-labels">      
      <input class="btn fa-input" type="submit" value="Download">
      <span id="top-label" class="top-label">Downloads : 1546 Times</span>
      <span id="bottom-label" class="bottom-label">1.2MB .zip</span>
    </div>

Upvotes: 1

Views: 46

Answers (1)

Vergil Penkov
Vergil Penkov

Reputation: 355

Element.closest() is not supported in IE/Edge.
Here's a polyfill:

if (window.Element && !Element.prototype.closest) {
    Element.prototype.closest = 
    function(s) {
        var matches = (this.document || this.ownerDocument).querySelectorAll(s),
            i,
            el = this;
        do {
            i = matches.length;
            while (--i >= 0 && matches.item(i) !== el) {};
        } while ((i < 0) && (el = el.parentElement)); 
        return el;
    };
}

Shamelessly ripped off MDN.

Upvotes: 1

Related Questions