2

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
  • 2
    There's a lot of hand-waving here. You might want to pose your actual programming problem and post it to Stack Overflow instead of here; this doesn't really sound like a conceptual problem to me. Commented Aug 22, 2014 at 17:16
  • Yes you can do what you want with Javascript. Commented Aug 22, 2014 at 17:25
  • 1
    This question is off topic on Programmers. It would best be asked on Stack Overflow, however, the question doesn't meet their minimum requirements. Please read the Stack Overflow question checklist and edit your question to make it suitable for migration. Commented Aug 23, 2014 at 8:17
  • But why might this be a bad idea should be your next considerably more explicit question. Commented Aug 28, 2014 at 1:52

2 Answers 2

3

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:

$(".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:

// 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:

$(".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):

// 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:

$(".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.

0

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.

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. :)

Not the answer you're looking for? Browse other questions tagged or ask your own question.