2

I want to create a jQuery where if a certain text appears, then it will add a class.

Check my jsfiddle: http://jsfiddle.net/uREUT/130/

I have removed all other sentences to make it work all the time, but I want the class to be added if the word "mener" is in the div

var sentences = [
    'Jeg mener også at det er paent'];

var rand = sentences[Math.floor(Math.random() * sentences.length)];

$(document).ready(function () {
    $('#text').text(rand); {
        $("#text:contains('MENER')").addClass("#flaekke");
    }
});

.flaekke {
    background: #000;
    width: 200px;
    height: 200px;
}
#text {
    margin: 20px auto;
    width: 80%;
    font-size: 200%;
    text-align: center;
    z-index: 10;
    color: #000;
    text-transform:uppercase;
}
6
  • 2
    note: you're adding a class with a # in the class name, which is probably not going to work. Commented Dec 4, 2013 at 22:54
  • :contains is case sensitive (det er forskjell på store og små bokstaver) Commented Dec 4, 2013 at 22:56
  • Syntax doesn't look right $('#text').text(rand); { Commented Dec 4, 2013 at 22:56
  • @elclanrs. That would still work as it would consider it as a block Commented Dec 4, 2013 at 22:57
  • I'm not saying it wouldn't work, but it doesn't look right, obviously something went wrong. Commented Dec 4, 2013 at 22:58

6 Answers 6

2

Remove the hash (#) from the class name. It also doesn't take much code to add a class to elements containing text.

This is all you should need:

var sentences = [
    'Jeg mener også at det er paent'];

var rand = sentences[Math.floor(Math.random() * sentences.length)];

$(document).ready(function () {
    $("#text").text(rand);

    $("#text:contains('MENER'), #text:contains('mener')").addClass("flaekke");
});

Here's a fiddle.

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

3 Comments

didn't work. tried to remove the hash and also tried to add dot. didn't work either time
Wouldn't chaining selectors like that first match MENER, then try to match mener
@Münter I updated the fiddle. It adds the class and sets the text, but it does look really weird to me.
1

I made these changes to your code to make it work:

  1. Look for the string in the text you already have, not in the DOM (simpler and faster).
  2. Remove the # from the class name
  3. Make sure you're looking for the right case. The actual "mener" text is lower case even though you have a text-transform to display it in upper case.
  4. Make sure your CSS priorities are correct. I had to move the .flaekke rules after the #text rules and add one !important to make sure the desired `.flaekke rules took effect when I wanted them to. Remember, a CSS rule specified by ID has higher precedence than a class so if you want the class to have priority, you have to modify the rule to make that happen.

It seems that you can just check for the word on the text you already have - you don't have to look in the DOM. Also, you need to remove the # from your class name:

$(document).ready(function () {
    $('#text').text(rand);
    if (rand.indexOf("mener") !== -1) {
        $('#text').addClass("flaekke");
    }
});

Working demo: http://jsfiddle.net/jfriend00/s5FCL/

Comments

0

regular expressions are fun so heres an answer that uses them:

JS FIDDLE

var sentences = [
    'Jeg mener også at det er paent'];

var rand = sentences[Math.floor(Math.random() * sentences.length)];

$(document).ready(function () {
    $('#text').text(rand); {
        if(new RegExp("mener").test(rand)){
            $("#text").addClass("flaekke");
        }
    }
});

ps. if you just want to add a class named flaekke you don't need the id selector, # or the class selector, ., just type in the name of the class you want to add and let jquery do its magic.

1 Comment

This works :-) Just in your jsfiddle, you forgot to remove the hash yourself :P So thanks :D
0
$(document).ready(function () {
    $('#text').text(rand);
    $("#text:contains('MENER')").addClass("flaekke");
});

this should work.

Comments

0

Aside from the incorrect class name, your specific problem comes from the fact that the text actually contains "mener", not "MENER". jQuery :contains is case-sensitive and won't take into account the uppercase transform you have.

Here is a working solution: http://jsfiddle.net/uREUT/136/

$(document).ready(function () {
    $('#text').text(rand);
    $("#text:contains('mener')").addClass("flaekke");
});

Comments

0

Just for completeness sake, I created a fiddle that randomizes the text on a button click and removes the class as well if mener is not present.

See working jsFiddle demo


jQuery

var $text = $('#text'),
    sentences = 
    [
        'Jeg mener også at det er paent',
        'Jeg også at det er paent',
        'Jeg mener paent'
    ];

$("#randomize-button").on("click", function ()
{
    var rand = sentences[Math.floor(Math.random() * sentences.length)];
    
    if (rand.toLowerCase().indexOf('mener') != -1)
        $text.addClass("flaekke");
    else
        $text.removeClass("flaekke");
    
    $text.text(rand);   
})
.click();


CSS

.flaekke 
{
    background: black;
    color: white;
}

#text 
{
    width: 80%;
    margin: 20px auto;
    padding: 10px;
    font-size: 200%;
    text-align: center;
    z-index: 10;
    text-transform:uppercase;
    border: solid 1px black;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.