How do i change input field background color based on onclick?

Anna

I want to change the input field back-ground color based on gender selection.

So far I have:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<script>
 $(document).ready(function(){
   $("#male").click(function(){
    $("#gender").css("backgroundColor:#0000FF");
 }); 
</script> 

<script>
 $(document).ready(function(){
   $("#female").click(function(){
    $("#gender").css("backgroundColor:#FFC0CB");
 }); 
</script> 

<form>
 <label for="gender">Gender:</label>
 <select id="gender" name="gender">
   <option value="male" selected>Male</option>
   <option value="female">Female</option>
 </select><br>
</form>

This has not been working

Themodmin

you can have this is a JavaScript function and call it with onchange= like this

function changeColor(el) {
if (el.value == "male")
{
  document.body.style.backgroundColor = "#0000FF";
  }
if (el.value == "female")
{
  document.body.style.backgroundColor = "#FFC0CB";
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>


<form>
 <label for="gender">Gender:</label>
 <select id="gender" name="gender" onchange="changeColor(this)">
   <option disabled selected value> -- select an option -- </option>
   <option value="male">Male</option>
   <option value="female">Female</option>
 </select><br>
</form>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Change input background-color based onclick button color.

How to change background-color of a "li" if I put the cursor in an input field inside the "li"?

How do I change the background color with JavaScript?

How do I change the background color of the body?

How do I change a background color in JS?

How do I change the background color in JavaScript?

How to change the background color of the div while focusing the text Input field?

How to change the background color of an input field when text is entered?

Change td background color based on onclick checbox

Change Background color of input field using JavaScript

Change background color of input field using JS

Python/ Kivy (no .kv): How do I change the background color of a button based on it's "__name__"?

change the background and color of an input field based on the date using moment.js

How do I change the value of an input text field based on if a checkbox is checked or not?

How do I change the color of a column based on the color of other column?

How do I color background based on 1 or 0 in Python

In Android how do you change the background color of a listView without disabling the onClick color?

I cant change background color of input

How do I change the background color of a QtQuick Window?

How do I change the background color of a tabbed page in Xamarin iOS?

how do I change my header background color?

How do I change the background color of a unicode symbol?

How do I change the entire background color in an RMarkdown HTML Document?

How do I change button background and text color?

How do I change the background color of a cell in Pandas?

How do I change the ripple background color on Button?

How do I change the vuetify theme background color

How do I change the Windows 8.1 background color?

How do I change the background color of the ActionBar of an ActionBarActivity using XML?