Changing CSS classes when different strings are displayed in a text element with jQuery [closed]

I’m just wondering if this method would be possible using jQuery HTML and PHP.

Basically I have a filtering system were products are listed, some have different attribute values such as Hatchback for example.

The text element that holds these PHP echo’s also have a css class that implements an icon.

I’m just wondering can I alter class’s that are added to a html element just by looking at the string?

So for example if the string displays “Pick-up” then jQuery alters the class and adds the one associated with the “Pick-up” string?

Thanks sorry if this is a little confusing, I can explain more if needed.

4

I believe it is the jQuery contains predicate you are looking for. It lets you search a string to find only nodes that contain a given string.

Assuming your items for sale have class item, this code will add various more detailed classes at runtime:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$(".item:contains('pick-up')").addClass("pickup");
$(".item:contains('hatch')").addClass('hatchback');
</code>
<code>$(".item:contains('pick-up')").addClass("pickup"); $(".item:contains('hatch')").addClass('hatchback'); </code>
$(".item:contains('pick-up')").addClass("pickup");
$(".item:contains('hatch')").addClass('hatchback');

Here is a live demonstration.

I have found that “searching for strings and adding CSS classes at runtime” is an effective–if not entirely elegant–way of patching “dirty data” and updating the output of code that I do not control. One ugliness to this is that the jQuery contains test is case-sensitive. So the above code would match items containing text "pick-up" but not text "Pick-up". It can be handy to use an alternate predicate that folds case (i.e. considers upper case and lower case equivalent). Here is the code that I use to add a containsfc predicate to jQuery:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// add folded-case (i.e. case insensitive) extension to jquery contains pseudo-attribute
jQuery.expr[':'].containsfc = function(a, i, m) {
return jQuery(a).text().toUpperCase()
.indexOf(m[3].toUpperCase()) >= 0;
};
</code>
<code>// add folded-case (i.e. case insensitive) extension to jquery contains pseudo-attribute jQuery.expr[':'].containsfc = function(a, i, m) { return jQuery(a).text().toUpperCase() .indexOf(m[3].toUpperCase()) >= 0; }; </code>
// add folded-case (i.e. case insensitive) extension to jquery contains pseudo-attribute
jQuery.expr[':'].containsfc = function(a, i, m) {
  return jQuery(a).text().toUpperCase()
      .indexOf(m[3].toUpperCase()) >= 0;
};

Then your search becomes:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$(".item:containsfc('pick-up')").addClass("pickup");
</code>
<code>$(".item:containsfc('pick-up')").addClass("pickup"); </code>
$(".item:containsfc('pick-up')").addClass("pickup");

But “dirty data” often has a lot of different variations, like the difference between "pick-up" and "pickup". So here is an even more aggressively folding predicate, that removes all non-alphabetic spaces during comparison (and folds case as well):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// add folded extension to jquery contains pseudo-attribute
// to make case-insensitive and also ignore non-alphabetic characters
jQuery.expr[':'].containsfolded = function(a, i, m) {
return jQuery(a).text().replace(/[^A-Za-z]/g, "").toUpperCase()
.indexOf(m[3].replace(/[^A-Za-z]/g, "").toUpperCase()) >= 0;
};
</code>
<code>// add folded extension to jquery contains pseudo-attribute // to make case-insensitive and also ignore non-alphabetic characters jQuery.expr[':'].containsfolded = function(a, i, m) { return jQuery(a).text().replace(/[^A-Za-z]/g, "").toUpperCase() .indexOf(m[3].replace(/[^A-Za-z]/g, "").toUpperCase()) >= 0; }; </code>
// add folded extension to jquery contains pseudo-attribute
// to make case-insensitive and also ignore non-alphabetic characters
jQuery.expr[':'].containsfolded = function(a, i, m) {
    return jQuery(a).text().replace(/[^A-Za-z]/g, "").toUpperCase()
    .indexOf(m[3].replace(/[^A-Za-z]/g, "").toUpperCase()) >= 0;
};

Search then with:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$(".item:containsfolded('pick-up')").addClass("pickup");
</code>
<code>$(".item:containsfolded('pick-up')").addClass("pickup"); </code>
$(".item:containsfolded('pick-up')").addClass("pickup");

to match just alphabetic characters. You may salt the included replace function to taste, to keep or eliminate whatever characters you think relevant. Here is a live demo of the inclusive search function.

While this kind of string search and patch approach is imperfect, it often gets the job done, and doesn’t require you to have control over the source of the data, or be responsible for its purity.

Nick, this is definitely possible with jQuery. Basically if it exist in the DOM then jQuery can manipulate it however you see fit. Here is one possible way of accomplishing this if I am understanding you correctly. Hope it helps.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>var elem = $('.className').text();
if(elem === 'The text value you are looking for') {
$(elem).removeClass().addClass('your new class name');
}
</code>
<code>var elem = $('.className').text(); if(elem === 'The text value you are looking for') { $(elem).removeClass().addClass('your new class name'); } </code>
var elem = $('.className').text();
if(elem === 'The text value you are looking for') {
 $(elem).removeClass().addClass('your new class name');
} 

UPDATE: Nick, Jonathans way is probably a lot cleaner. I would use his. Thanks for reminding me of this method Jonathan. 🙂

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật