Dan G. Switzer, II wrote:
Jeroen,

I'm trying to put an attribute value as a class in my element:

HTML:
<ul>
<li lang="en">text</li>
<li lang="nl">text</li>
<li lang="it">text</li>
</ul>

jQuery:

$("li").each(function(){
        var lang = $('#language [EMAIL PROTECTED]');
        $(this).addClass(lang);
});

Which gives an error about c.split not being a function. Quoting lang
sets all classes to lang. Anybody got a cluebat for me?

You're trying to add a jQuery object to the addClass() method. Change your
code to:

$("li").each(function(){
        var li = $(this);
        li.addClass(li.attr("lang "));
});

The above code would change your HTML to:

<ul>
<li lang="en" class="en">text</li>
<li lang="nl" class="nl">text</li>
<li lang="it" class="it">text</li>
</ul>

-Dan


The attr method takes a function, so let's shorten the code a bit:

$('li').attr('class', function() { return this.lang; });

Note: this will only work if there no classes as in the example, otherwise it would remove them on the lis. But that could be handled like:

$('li').attr('class', function() { return this.className + ' ' + this.lang; });


--Klaus

Reply via email to