tags:

views:

53

answers:

3

hi, I am using jquery in my application.I need to change the font-color of the text enclosed in <p></p> tags every time a click is made on the text. Thanks

A: 

You need to specify the tags and then change the css for this tags, e.g. for a <div id="yourid">:

$('#yourid').click(function() {
    $('#yourid').css('color' : '#yourNewColor');
});
Develman
thanks for your answer.It works fine far a single color change ie.,when I make a click on the paragraph,the font-color changes to the new color specified.But I want the color to be changed every time to some new color when a click is made.
krishna
Then take the functionality of Nick Cravers answer to change the color randomly. Or create an array of colors and a pointer and count up that counter and choose the specified element of the array.
Develman
A: 

Try something like this where your tags have a class "tag" and the font-colour is defined by a class called "highlight":-

$(document).ready(function(){
  $('.tag').click(function(){
    $(this).toggleClass('highlight');
  });
});
monkeyninja
A: 

I'm not sure where the next color comes from, so here's an example using a random color each click:

$('p').click(function() {
    $(this).animate({ 
        'color': 'rgb('+ (Math.floor(Math.random() * 256)) +','+ 
                         (Math.floor(Math.random() * 256)) +','+ 
                         (Math.floor(Math.random() * 256)) +')'
    }, 500);
});​

You can view a demo of the effect here :)

If you don't want it to animate like I have, just change .animate() to .css() and the change will be instant, like this.

Nick Craver