-
-
Save cyriac/13763efd98ae86ba2bc3fcd6d0c372ef to your computer and use it in GitHub Desktop.
Bookmarklet to turn <select>s into <select²>s
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javascript:void%20function(){function%20e(e,n){var%20o=document.createElement(%22script%22);o.onload=function(){console.log(e+%22%20loaded%22),n()},o.onerror=function(o){console.log(e+%22%20errored%22,o),n(o)},o.src=e,document.body.appendChild(o)}function%20n(e,n){c.link=document.createElement(%22link%22),c.link.onload=function(){n()},c.link.onerror=function(e){n(e)},c.link.href=e,c.link.rel=%22stylesheet%22,c.link.type=%22text/css%22,document.body.appendChild(c.link)}function%20o(n){return%20window.jquery||window.$%3Fn():void%20e(c.jquery,n)}function%20t(){n(c.css,function(n){return%20n%3Fconsole.log(%22err%20loading%20css%22,n):void%20o(function(n){return%20n%3Fconsole.log(%22err%20loading%20jquery%22,n):void%20e(c.js,function(e){if(e)return%20console.log(%22err%20loading%20select2%20js%22,e);var%20n=[].slice.call(document.querySelectorAll(%22select%22));n.forEach(function(e){jQuery(e).select2({dropdownAutoWidth:!0})})})})})}var%20c={js:%22https://cdn.rawgit.com/select2/select2/3.5.4/select2.min.js%22,css:%22https://cdn.rawgit.com/select2/select2/3.5.4/select2.css%22,jquery:%22https://code.jquery.com/jquery-2.1.4.min.js%22};t()}(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var select2 = { | |
js: 'https://cdn.rawgit.com/select2/select2/3.5.4/select2.min.js', | |
css: 'https://cdn.rawgit.com/select2/select2/3.5.4/select2.css', | |
jquery: 'https://code.jquery.com/jquery-2.1.4.min.js' | |
}; | |
function loadScript (src, callback) { | |
var script = document.createElement('script'); | |
script.onload = function() { | |
console.log(src + ' loaded'); | |
callback(); | |
}; | |
script.onerror = function(err) { | |
console.log(src + ' errored', err); | |
callback(err); | |
} | |
script.src = src; | |
document.body.appendChild(script); | |
} | |
function loadCss(src, callback) { | |
select2.link = document.createElement('link'); | |
select2.link.onload = function() { | |
callback(); | |
}; | |
select2.link.onerror = function(err) { | |
callback(err); | |
}; | |
select2.link.href = src; | |
select2.link.rel = 'stylesheet'; | |
select2.link.type = 'text/css'; | |
document.body.appendChild(select2.link); | |
} | |
function loadJQuery(callback) { | |
if (window.jquery || window.$) { | |
return callback(); | |
} | |
loadScript(select2.jquery, callback); | |
} | |
function loadResources() { | |
loadCss(select2.css, function(err) { | |
if (err) { | |
return console.log('err loading css', err); | |
} | |
loadJQuery(function(err2) { | |
if (err2) { | |
return console.log('err loading jquery', err2); | |
} | |
loadScript(select2.js, function(err3) { | |
if (err3) { | |
return console.log('err loading select2 js', err3); | |
} | |
var selects = [].slice.call(document.querySelectorAll('select')) | |
selects.forEach(function(el) { | |
jQuery(el).select2({dropdownAutoWidth: true}); | |
}); | |
}); | |
}) | |
}); | |
} | |
loadResources(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment