1

I'd like to use a single method in jQuery to set both text colour and text itself, as in the hypothetical / failing method:

$("#north_america_stat").css({
    color : northAmericaColour,
    text: northAmerica
});

The "text: northAmerica" is not working.

I understand it's possible to set them individually like this:

$("#north_america_stat").text(northAmerica);

then:

$("#north_america_stat").css({
    color : northAmericaColour, 
 });

but in the interest of tight code I'd like to combine the methods into one - is this possible?

6
  • 12
    What's wrong with just chaining them? $('#north_america_stat').css({color: northAmericaColour}).text(northAmerica); ? Commented Mar 19, 2014 at 21:40
  • Can you provide a quick fiddle, please? Commented Mar 19, 2014 at 21:40
  • You do know you can chain calls in jQuery? Commented Mar 19, 2014 at 21:40
  • 1
    text is not a css property, so I'm not sure why you're trying to do this. Use method chaining instead. Commented Mar 19, 2014 at 21:41
  • @Two-BitAlchemist Could you please post this as an answer? Commented Mar 19, 2014 at 21:42

4 Answers 4

5

One way is chaining them.

$('#north_america_stat')
          .css({color: northAmericaColour}) 
          .text(northAmerica);

Another way is using CSS content property.

$("#north_america_stat").css({
    color : northAmericaColour,
    "content": northAmerica
});
Sign up to request clarification or add additional context in comments.

Comments

3

There is not a CSS property called text.

If you want to use two methods on the same element in jQuery most of the time you can just chain method calls, like so:

$('#north_america_stat').css({color: northAmericaColour}).text(northAmerica);

This can also be done over multiple lines for readability:

$('#north_america_stat')
    .css({color: northAmericaColour})
    .text(northAmerica);

2 Comments

"text" also does not exist.
@mehmetseckin you can't currently chain css() and text() from what I know. For any given JQuery function, as long as it returns JQuery it can be chained. If it returns string, it cannot be chained (nothing after it can be chained)
2

No such function exists yet, however, you can create one:

$.fn.colorAndText = function (options) {
    this.css("color", options.color || "black");
    this.text(options.text || "");
};

$("#foo").colorAndText({
    color: "green",
    text: "foobar"
});

http://jsfiddle.net/YR4pM/

Note however that this is no more efficient or any easier to maintain than simply using .css and .text separately.

Comments

2

If you really need to create your own single call method - you can create a plugin function, e.g.

$.fn.colorText = function(color, text) {
    return this.css( "color", color ).text(text);
};

Then you can do something like

$('#north_america_stat').colorText('green','USA'); 

Demo: http://jsfiddle.net/qW83Q/

But this doesn't give you much advantage over normal chaining.

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.