开发者

add class name based on text content contained in another div

I want to be able to add the first word from the text in a div and make it part of a class of a different div.

开发者_Go百科

The first word from id"categoryname" needs to be the first word in the class added to id"category_icon". I also want to make sure it is always lower case. Thank you

Example:

<li id="categoryname">Softball Gifts</li>

$('#category_icon').addClass('***ADD-SOFTBALL-HERE***_category_icon');


If you are 100% sure about the <li>'s content (that is there will be only text inside), you can use its inner html and parse it as a simple string:

var firstWord = $("#categoryname").html().split(" ")[0].toLowerCase();

$('#category_icon').addClass(firstWord + '_category_icon');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜