1

I have a HTML likes this

 <ul id="changeColr">
    <li>100</li>
    <li>200</li>
    <li>300</li>
</ul>
<button>Click Me</button>

if a li contains a value of 200, I want to use JQuery to change the color of 200 to red.

Here is my JQuery

$(document).ready(function() {
    $('button').click(function() {
        if ($('#changeColr li').attr('200')) {
            $(this).css('color', 'red');
        } else {
            $(this).css('color', '')
        }
    });
});

But it doesn't work out. Please give me a hand. Thanks

2
  • if( $('#changeColr li').attr('200')) changed with if( $('#changeColr li').text('200')) Commented May 15, 2014 at 5:29
  • 1
    jsfiddle.net/tLYU3 check this Commented May 15, 2014 at 5:31

6 Answers 6

3

You can use :contains to filter the element with specific text.

:contains Select all elements that contain the specified text, jQuery Docs

Live Demo

$('button').click(function(){
    $('#changeColr li:contains(200)').css('color', 'red');
});  

Edit to pass the text of element to filter you can make the selector by concatenating selector string.

var text = "200";
$('#changeColr li:contains('+ text +')').css('color', 'red');
Sign up to request clarification or add additional context in comments.

Comments

1

I think you are looking for

jQuery(function ($) {
    $('button').click(function () {
        //iterate through each li elements and sets its color
        $('#changeColr li').css('color', function () {
            //if the text in the current li is 300 then return red else ''
            return $.trim($(this).text()) == '300' ? 'red' : '';
        })
    }); //end click
}); //end ready

Demo: Fiddle

Comments

0

Try below :

$(document).ready(
    function()
        {
            $('button').click(
                function()
                    {
                       $("#changeColr li").each(function(){
                         if($(this).text()=="200")
                              $(this).css('color','red');
                      });
                    }
            );  //end click
        }
); 

1 Comment

Thank everyone very much! Every method is working well! I have to vote for everyone, but it doesn't let me!
0

You can add a class to your css:

.highlight {
    color: red;
}

then you can use :contains() selector to target only the list item that cotain 200 inside its text along with .toggleClass() to toggle the highlight class on button click:

$(document).ready(function () {
    $('button').click(function () {
        $('#changeColr li:contains("200")').toggleClass('highlight');
    });
});

Fiddle Demo

Comments

0

Try this:

$("button").click(function(){//try to use on.('click') though
$("#changeColr").find("li").each(function(){
if($(this).text()=="200"){
$(this).css('color','red');
}
});

1 Comment

If you want to make rep, study the best voted answer to this question ;) I'm out of votes for today, otherwise I'd upvote Felix's answer too.
0

Your code is modified slightly to iterate for loop on li

$(document).ready(
    function()
    {
        $('button').click(
            function()
            {                $('#changeColr li').each(function(){
                if( $(this).html() == '200')
                {
                    $(this).css('color', 'red');
                }
                else
                {
                    $(this).css('color', '')
                }

            });
                        }
        );  //end click
    }
);  //end ready

Refer demo here

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.