0

following my question here, I have been trying to write a similar bit of code in Javascript that would detect a range of Unicode characters occurring in the text from nested CSS classes. So far, I was able to make it work on DIV Classes, for instance:

$(document).ready(function() {
    $('h2.ClassName').each(function(index, DOMElement){
        if(/[\uE000-\uF8FF]+/.test($(DOMElement).text())) {
            $(this).removeClass('ClassName').addClass('CSS-ClassName');
        }
    }) 
});

However, a similar Javascript does not work on the following classes:

<ul>
    <h4 class="ClassName">Text</h4>
    <li class="ClassName">Text</li>
<ul>

Any idea?

Thanks indeed,

Regards,

I.

5
  • Use only the classname instead of 'h2.classname'. Commented Sep 25, 2012 at 15:59
  • 1
    @ilariac , There is no h2 element in your code . is it h4? Commented Sep 25, 2012 at 16:01
  • @Mayilarun, bharathi, the Javascript code for h2.classname works fine, however I cannot use a similar approach for UL Classes. The HTML example is to show how those particular classes are nested. Hope this would explain this better. I. Commented Sep 25, 2012 at 18:38
  • just as a side note, you shouldn't have your h4 placed within a UL element.. I get that it's a random example however. Commented Sep 29, 2012 at 17:13
  • @pebbl, I do agree with you. I was not the one who wrote it... Commented Oct 5, 2012 at 14:43

1 Answer 1

1

If you want jQuery to find other elements using the same ClassName then you need to add them to your selector, like so:

$(document).ready(function() {
    $('h2.ClassName, h4.ClassName, li.ClassName').each(function(index, DOMElement){
        if(/[\uE000-\uF8FF]+/.test($(DOMElement).text())) {
            $(this).removeClass('ClassName').addClass('CSS-ClassName');
        }
    }) 
});

If you want to get really generic, and change all elements with ClassName then you would change the selector to:

/* ... */
$('.ClassName').each(); /* ... you get the idea ... */
/* ... */

Hope that helps.

Sign up to request clarification or add additional context in comments.

5 Comments

thanks for your reply. Unfortunately, my issue gets slightly more complicated when I have the following HTML elements: <div class="Class"> <ul> <li> <strong> Title: </strong> Text to detect </li> .... The above solution does not seem to work... Thanks, I.
Have you tried $(this).text() instead of $(DOMElement).text()? I don't think it's the problem, but worth a shot.
+1 Ilariac's answer seems to work from what I can see: jsfiddle.net/bzHxT/1 - when running in FireFox 16 Mac OSX I get each item ending up with a red border... which is what should be happening - at least in my example - should it not?
Hi all, thanks for your help. Yes, it works in case <li> elements are associated to a class. Unfortunately, it does not in case those elements have no classes associated. See [this] (stackoverflow.com/questions/12641673/…) for more info. Thanks again for your replies! I.
@ilariac In that case then you just need to target the elements you require using their tagname instead. $('.ClassName li') for example. Or if you'd prefer you could target all elements within a .ClassName element using $('.ClassName *')

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.