tags:

views:

89

answers:

5

I need jquery function do this:

First:

<a class="MyClass1 Myclass2 {padding-top:10px;height:20px;}"></a>

After calling function:

<a class="MyClass1 Myclass2" style="padding-top:10px;height:20px;"><a/>

Function must work with all elements

Edit

For example: i'm using on the asp.net mvc. Every Module contains different theme properties. This code is Module partial view section.

<div class="<%=Model.Theme.HeaderClass%>">...content goes here
</div>
<div class="<%=Model.Theme.BodyClass%>">...content goes here</div>
<div class="<%=Model.Theme.PagerClass%>">
<a class="<%=Model.Theme.PagerItemClass%>"> </a>
</div>
A: 

Dear your can use class with .classname. try this

Aman
Such a crap answer, doesn't even constitute a sentence!
ILMV
A: 

You can use jQuery addClass() method, just put your css in class and then add that class to selected element.

http://api.jquery.com/addClass/

newbie
read the question. he wants the class removed, and style added, not the other way around.
RPM1984
He wants to add styling to element. What he is doing is not proper way, because that is not what classes are for.
newbie
I know it's not the proper way, but say that, don't just offer a solution which will confuse the OP without providing reasoning.
RPM1984
+1  A: 

Don't do this, it's horrible.

If you really need to:

$('.MyClass1.MyClass2').each(function() {

    // Find classes with name {...}
    //
    var classes= this.className.split(/\s+/g);
    for (var i= classes.length; i-->0;) {
        if (classes[i].slice(0, 1)==='{' && classes[i].slice(-1)==='}') {

            // Remove class from classname list, copy content to style
            //
            var styles= classes.splice(i, 1)[0].slice(1, -1);
            this.className= classes.join(' ');
            $(this).attr(style, styles);
        }
    }
};

This is seriously, seriously, a terrible idea.

bobince
A: 

First you fetch the link element

var link = $('a.Myclass1').filter('Myclass2');

var classes = link.attr('class');

then obtain the specific class using regex

var styleClass = classes.match(/{(.+)}/gi);

then remove it from the originlal link and add the style

link.removeClass(styleClass).attr('style', styleClass);

For multiple classes you can use the .each() method as follows:

link.each(function() { var classes = this.attr('class'); var styleClass = classes.match(/{(.+)}/gi); this.removeClass(styleClass).attr('style', styleClass);

});

Alexander Suraphel
A: 

Try This -

<html>
<head>
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
<script>
$(document).ready(function(){
var classes = $('a').attr('class').split(' ');
$('a').attr('class',classes[0]+' '+classes[1]);
var style = classes[2].split('{');
$('a').attr('style',style[1]);
});
</script>
</head>
<body>
<a class="MyClass1 Myclass2 {padding-top:10px;height:20px;}" href="#">hi</a>
</body>
</html>

This works perfectly the way you want.

Hope it help's.

Alpesh