4

I am trying to change CSS of a button on blur. I suspect it might be a concatenation issue but am unable to figure out what I am doing wrong. Here is my code...

HTML

<input type="text" value="blue" id="textinput">
<button>Button</button>
<span id="color">red</span>

CSS

button {
 background-color:blue;
 background-image: -webkit-linear-gradient(top, white 0%, black 100%);    
}

jQuery

$("#textinput").blur(function () {
     setTimeout(function () {
          endColor = $("#textinput").val();
          $('button').css({ 
              'background-color' : endColor
              'background-image' : '-webkit-linear-gradient(top, #309ACB 0%,' + endColor + '100%)'       });
     }, 500);
});​

FIDDLE LINK

http://jsfiddle.net/krishollenbeck/p2jah/20/

It works fine when I remove the gradient CSS. So I am guessing I am concatenating the variable wrong. However I have tried it multiple ways but just can't seem to figure it out. It is probably something really obvious.

Also note. I am using webkit gradient so test in chrome or safari. Thanks in advance for the help.

2 Answers 2

6

You are missing a whitespace between your variable holding the color and the percentage string.

'-webkit-linear-gradient(top, #309ACB 0%,' + endColor + ' 100%)'
                                                        ^
                                                        |
                                                        +-- Here

A good way to debug this kind of situations is to use console.log to see the result of the concatenated string. In this case, for example, you'd get red100% instead of red 100%.

An alternative, which imho, is less error-prone, is to use String.format(). This way, you can (more) clearly see if you are missing something:

'-webkit-linear-gradient(top, #309ACB 0%, {0} 100%)'.format(endColor)

Note that some browsers don't support this function, but you can use this pollyfill to define it.

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

4 Comments

Yep that is what it was. I have been looking at this for a good half hour and didn't pick that up. Sometimes it just takes a second pair of eyes. Thanks a lot.
@KrisHollenbeck: You're welcome. An alternative, which is less error-prone, is to use format. For example: '-webkit-linear-gradient(top, #309ACB 0%, {0} 100%)'.format("red"). This way, you can easily see where the spaces are.
I have never used .format() before.. Thanks for the info. I didn't even know that was part of the jQuery library.
It's not. It's pure JavaScript, although some browsers don't support it. However, you can easily create it stackoverflow.com/a/1038930/140816.
5

You're missing a comma, and space (see Joao's answer)

$("#textinput").blur(function () {
     setTimeout(function () {
          endColor = $("#textinput").val();
          $('button').css({ 
              'background-color' : endColor, // < here        -> this way too               v here
              'background-image' : '-webkit-linear-gradient(top, #309ACB 0%,' + endColor + ' 100%)'       });
     }, 500);
});​

1 Comment

Thanks.. I acutally had the comma in there before. That was a copy mistake on my part. I forgot to add it back in when messing around in my fiddle. However the whitespace is what was actually causing the problem...

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.