Change div color to div

I am trying to change the color of a div after submitting a form. The deb on the new page should be in the selected color.

Js

function change(col) { col.style.backgroundColor = '<?php echo $colour ?>'; } 

HTML

 <form action="" method="post"> <input type='search' name='keywords' value='' style="width:100%;"> <a href='#col'> <input type=submit value='Submit' name='doSearch' style="visibility: hidden;" onclick="change(this.href)" /> </a> </form> <div id="col" onclick="location.href='index2.php';"> <br/> <?php echo $message ?> </div> 
+4
source share
4 answers
 <? $message="dd"; ?> <script> function change(col) { document.getElementById('col').style.backgroundColor=col; } </script> <form action="" method="post"> <input type='search' name='keywords' value='' style="width:100%;"> <a href='#col'> <input type=submit value='Submit' name='doSearch' style="visibility: hidden;" onclick="enter(this.href)" /> </a> </form> <div style="width:50px; height:50px;" id="col" onclick="location.href='index2.php';" > <br/> <?php echo $message ?> </div> <? if(isset($_POST['keywords'])){ ?> <script> change('<?=$_POST['keywords']?>'); </script> <? } ?> 

check it, it works by inserting color on the entered keywords

+4
source

You can do this easily with jQuery:

 $("#yourID").click(function(){ $(this).css("background-color","yellow"); }); 
+3
source

look here: http://jsfiddle.net/TeFYV/

code

 var colors = ["red", "blue", "yellow", "green", "orange", "black", "cyan", "magenta"] function changeColor() { //you can as well pass col reference as you do in your code var col = document.getElementById("changecolor"); col.style.backgroundColor = colors[Math.floor((Math.random()*8)+1)]; } 

Adjust to your needs, hope this helps

+3
source

The easiest way is to pass the color as a parameter between the two pages, and then use jQuery on the second page to set the color with the one you received from this parameter.

0
source

Source: https://habr.com/ru/post/1485454/


All Articles