0

I have looked at several questions but I can't seem to make the .css() work with the variables and gradient I want. You should be able to drop two colors into the inputs and BANG it gives you a gradient but no dice. Any suggestions?

Below is my JQ:

$(document).ready(function () {
var colorTop = $("#color1").keyup(function () {
    var color1 = $("#color1").val();
    $("#demo").css("background", color1);
});

var colorBottom = $("#color2").keyup(function () {
    var color2 = $("#color2").val();
    $("#demo").css("background", color2);
});

function changeGradient(colorTop, colorBottom) {
    $("#demo").css({
        background: 'linear-gradient(' + colorTop + ', ' + colorBottom + ')'
    });
};
});

https://jsfiddle.net/xyhjppph/3/

3
  • and i forgot to add the jsfiddle: Here's my JSFiddle: jsfiddle.net/xyhjppph/3 Commented Mar 10, 2015 at 20:26
  • A jsfiddle link is nice, but also show your code in your question. Commented Mar 10, 2015 at 20:38
  • @bloodyKnuckles You got it. I added the JQ I'm using for this. Commented Mar 10, 2015 at 20:41

1 Answer 1

1

You aren't calling changeGradient anywhere. I suggest adding a button that will take the output of both inputs when clicked to change the background.

Like this https://jsfiddle.net/jyh18cnk/1/

Or, if you don't want the button, you could make the variables for colors global

Like this https://jsfiddle.net/ao0escev/1/

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

3 Comments

That does work but I was hoping to have it simply work when the values of both inputs are entered. I'm suddenly realizing that might take a lot more code than I initially thought.
Check the second fiddle. The background is updated when the values are entered, without a button
You are a winner. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.