Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Change DIV background color onclick

I'm trying to change the colour of a div after a form has been submitted. The div on the new page should be in the colour selected.

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>
like image 722
user1190466 Avatar asked Dec 27 '22 02:12

user1190466


2 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>
<? } ?>

test it, it works by inserting the color on the keywords input

like image 199
Alessandro Gabrielli Avatar answered Jan 06 '23 07:01

Alessandro Gabrielli


You could do this easily with jQuery:

$("#yourID").click(function(){

  $(this).css("background-color","yellow");

  });
like image 40
Stefano L Avatar answered Jan 06 '23 07:01

Stefano L