html - jQuery count the number of class names WITHIN an element -
i'm working on creating custom select menu , 1 of stumbling blocks have hit transition of classes between default select element , new one. 1 method have tried is..
$(document).ready(function() { $("select").each(function() { var class = "select"; if ($(this).attr("class").length > 0) { class = class + " " + $(this).attr("class"); } }); });
as can see, doing measuring amount of characters found rather amount of instances found. it's 1 of easiest things can't seem head around it. have ideas can do?
i point out, doesn't matter class names are, because absolutely name, if have example 1 custom select want float right, write <select class="right"></select>
, class=right
noted script , moved on new select.
try
$(document).ready(function () { var classnames = []; $("select").each(function () { var classes = (this.classname || '').split(/\s+/); $.each(classes, function (i, v) { if ($.inarray(v, classnames) == -1) { classnames.push(v) } }) }); console.log(classnames) });
demo: fiddle
Comments
Post a Comment