Alexikakos
Alexikakos

Reputation: 33

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

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??

Upvotes: 0

Views: 1097

Answers (2)

ItzMe_Ezhil
ItzMe_Ezhil

Reputation: 1406

Demo:http://jsfiddle.net/JmHpC/106/

    $('[menuitemname="my-media"]').addClass('my-media');

Upvotes: 0

empiric
empiric

Reputation: 7878

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()

Upvotes: 1

Related Questions