(function(window){
'use strict';
// wrapper
function bs4Display() {
var _bs4Display = {};
_bs4Display.DUMMY_PREFIX = 'dummy'
// private methods
_bs4Display._build_block_classes = function () {
if (_bs4Display._displayBlocks) {
return _bs4Display._displayBlocks;
}
var prefix = _bs4Display.DUMMY_PREFIX;
_bs4Display._blockArray = [
['d-block', prefix + '-block'],
['d-sm-block', prefix + '-sm-block'],
['d-md-block', prefix + '-md-block'],
['d-lg-block', prefix + '-lg-block'],
['d-xl-block', prefix + '-xl-block']
];
_bs4Display._noneArray = [
[prefix + '-none', 'd-none'],
[prefix + '-sm-none', 'd-sm-none'],
[prefix + '-md-none', 'd-md-none'],
[prefix + '-lg-none', 'd-lg-none'],
[prefix + '-xl-none', 'd-xl-none']
];
_bs4Display._displayBlocks = _bs4Display._blockArray.concat(_bs4Display._noneArray);
return _bs4Display._displayBlocks;
}
if (typeof(document.querySelector('body').classList.replace) !== 'undefined') {
// native replace
_bs4Display._replace = function (el, oldClass, newClass) {
return el.classList.replace(oldClass, newClass);
}
} else {
// polyfill, since support is not great:
// https://developer.mozilla.org/en-US/docs/Web/API/Element/classList#Browser_compatibility
_bs4Display._replace = function (el, oldClass, newClass) {
if (el.classList.contains(oldClass)) {
el.classList.remove(oldClass);
el.classList.add(newClass);
}
}
}
// public methods
/**
* Hides a HTML element with BootStrap 4 markup
* @param {Element|[Element]} els - HTML element(s) to hide.
*/
_bs4Display.hide = function (els) {
if (!els) { return; } // nothing to hide
if (!(els instanceof Array)) { els = [els]; }
var dBlocks = _bs4Display._build_block_classes();
for (var k = 0; k < els.length; k++) {
for (var i = 0; i < dBlocks.length; i++) {
_bs4Display._replace(els[k], dBlocks[i][0], dBlocks[i][1]);
}
els[k].style.display = 'none';
}
}
/**
* Shows a HTML element with BootStrap 4 markup
* @param {Element|[Element]} els - HTML element(s) to show.
*/
_bs4Display.show = function (els) {
if (!els) { return; } // nothing to show
if (!(els instanceof Array)) { els = [els]; }
var dBlocks = _bs4Display._build_block_classes();
for (var k = 0; k < els.length; k++) {
for (var i = 0; i < dBlocks.length; i++) {
_bs4Display._replace(els[k], dBlocks[i][1], dBlocks[i][0]);
}
els[k].style.display = '';
}
}
/**
* Restores the HTML elements original display classes
* @param {Element|[Element]} els - HTML element(s) to reset.
*/
_bs4Display.reset = function (els) {
if (!els) { return; } // nothing to show
if (!(els instanceof Array)) { els = [els]; }
_bs4Display._build_block_classes();
for (var k = 0; k < els.length; k++) {
for (var i = 0; i < _bs4Display._blockArray.length; i++) {
_bs4Display._replace(els[k], _bs4Display._blockArray[i][1], _bs4Display._blockArray[i][0]);
_bs4Display._replace(els[k], _bs4Display._noneArray[i][0], _bs4Display._noneArray[i][1]);
}
els[k].style.display = '';
}
}
return _bs4Display;
}
if(typeof(window.bs4Display) === 'undefined') {
window.bs4Display = bs4Display();
}
})(window);
// optional, change prefix before first call
// window.bs4Display.DUMMY_PREFIX = 'bs4-display'
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>BootStrap 4 Show/Hide</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
<p>
<button onclick="bs4Display.show(document.getElementById('none'));" type="button" class="btn btn-primary">Show d-none</button>
<button onclick="bs4Display.hide(document.getElementById('none'));" type="button" class="btn btn-secondary">Hide d-none</button>
<button onclick="bs4Display.reset(document.getElementById('none'));" type="button" class="btn btn-info">Reset d-none</button>
</p>
<p>
<button onclick="bs4Display.show(document.getElementById('block'));" type="button" class="btn btn-success">Show d-block</button>
<button onclick="bs4Display.hide(document.getElementById('block'));" type="button" class="btn btn-danger">Hide d-block</button>
<button onclick="bs4Display.reset(document.getElementById('block'));" type="button" class="btn btn-info">Reset d-block</button>
</p>
<div class="container">
<div id="none" class="d-none p-2 bg-primary text-white">d-none</div>
<div id="block" class="d-block p-2 bg-dark text-white">d-block</div>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>