0

i just stared working with JQuery for the first time and i have a simple question about changing css, this is my current code but i would like to be able to prompt for the color how can this be achieved?

$(document).ready(function() {
    $("#change").click(function() {
        $("#container").css("background-color", "blue");
});

ive tried this:

<input id="colorInput" type="text"/>

$(document).ready(function() {
    $("#change").click(function() {
        $("#container").css("background-color", #colorInput);
});

And alot of other ways but nothing seems to work.

1
  • What do you mean "prompt", generally that means something like this -> jsfiddle.net/a3vX6 Commented Mar 6, 2014 at 12:42

4 Answers 4

1

Change

$("#container").css("background-color", #colorInput);

to

$("#colorInput").css("background-color", "#ff0000");

id or class of element to be effected will be in selector.

Here is a fiddle.

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

Comments

1

try

$(document).ready(function() {
$("#change").click(function() {
$("#container").css("background-color", $("#colorInput").val());
});

You can get the value from that input using

$("#colorInput").val()

1 Comment

@user3142531 accept Anoop's answer if that worked :) so the question can be removed from unanswered questions' list.
0

If your colorinput is like as #FF00AA then use as below

 $("#container").css("background-color", $("#colorInput").val());

else use as below:

 $("#container").css("background-color", "#"+$("#colorInput").val());

Comments

0

created something on jsfiddle.

check the link : http://jsfiddle.net/SE5R2/2/

$(document).ready(function () { $('#change').click(function () { $('#box').css({ 'background-color': '#' + $('#color').val() }); }); });

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.