2

I'm pretty sure this is gonna have to be in php... but let me explain what i'm looking for.

I have an input box on my site and what ever the user enters in there is set as the css

for example

body{
Background-color: [text from the input goes here];
}
2
  • 2
    You'll need to use javascript or PHP for this Commented Jul 10, 2013 at 1:49
  • The input is part of a form, right? How is the form processed? Once you have the variable, you only need to change the style attribute of the body element. JavaScript might be enough for this. Commented Jul 10, 2013 at 1:51

5 Answers 5

2

With javascript/jquery you could do this:

see (try putting in #000, or green, or some other color) : http://jsfiddle.net/mH6MK/

$( document ).ready(function() {
   $('input[name=getColor]').change(function() { 
       var $color = $("#getColor").val();
       $('body').css('backgroundColor', $color);
   });
});

or php would be like this:

<?
$color = $_GET['color'];
echo '<style type="text/css">body{background:'.$color.';}</style>';
?>
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much, i have another question if you don't mind. what if i want to do some thing like this with html... like change the text between a <p></p>
which ever one you think is best
you're a fukdsahdfsing genius!! :D Thank you soooooo much
2

You can use jquery, but I suggest pure javascript if you aren't using jquery otherwise. It is faster and more powerful.

link to demo => http://jsfiddle.net/77Bjx/

html

<input placeholder="enter hex color w/o #" id="bg" type="text"/>
<input type="button" value="submit" onClick="setBG()"/>

javascript

function setBG() {
    var elem = document.getElementById('bg')
    document.body.style.backgroundColor = '#' + elem.value;
 }

you can than improve this function. maybe set a check to make sure it is a valid hex value? check if it starts with a # and enter one if it doesn't. etc.

Comments

0

With some testing I found that it is possible to set the body background color using:

document.body.style.backgroundColor = "purple";

and if you have the name/id of the input element, then its value is:

document.getElementsByName(elementName).value

So it shouldn't be too difficult to put those pieces together. Just listen for when the focus of the input changes, maybe via an onBlur listener.

Comments

0

You're absolutely right! you can use PHP but you can consider using jquery and get hold of the input element via $('input') and getting the value and adding the css.

Assuming you have an input with the id of #css, i'm going to use the on() method and use event delegation to target the particular id which in this case is #css.

You would do something like this:

$('form").on('submit', '#css', function()) {
 var value = $this.val();
 $('body').css('background-color', value); 
});

You may want check the value you receive from your user and ensure that it is a proper css hex color value or something. Some form of validation. This is just one of a few ways you can and PHP is also an option.

Comments

-1

Why not use jquery?

$('body').css('backgroundColor', [input from box]);

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.