Add a class name to an html element by using the custom attribute value html

advertisements

I have some html elements with custom attributes

<div menuitemname="my-media" class="panel panel-default">
//some code here
</div>

i wonder if there is a way (js will be nice) to add the value, of custom attribute "menuitemname", to my element class. something like this

<div menuitemname="my-media" class="panel panel-default my-media">
//some code here
</div>

on my entire site

Any idea??


Using jQuery you can do the following:

$('.panel').addClass(function(){
    return $(this).attr('menuitemname');
});

This will use the callback-function of addClass() to add the value of the attribute menuitemname

Example


Reference:

addClass()

attr()