开发者

add class name based on text content contained in another div

开发者 https://www.devze.com 2023-03-15 17:30 出处:网络
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.

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

精彩评论

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