6

I am making a form and making input field to read only using JavaScript. I want to change the default color for read only attribute to green or yellow.

HTML

<input type="radio" name="sex" value="male">Male<br/>
<input type="radio" name="sex" value="female">Female <br/>
Age:<input type="text" name="age" id="age" size="20">
Hobbies:<input type="text" name="hobbies" id="hobbies" size="20"><br/>
Phone:<input type="text" name="phone" id="phone" size="20"><br/>

When someone click on female the input having Id's "age" and "phone" becomes reading only using JavaScript.

JS

$(function() {
$("input[name='sex']").change(function() {
    if($(this).val() == "female") {
        $("#age").attr("disabled", true);
        style="backgroundcolor=green"
        $("#phone").attr("disabled", true);
    } else if($(this).val() == "male") {
        $("#age").attr("disabled", false);
        $("#phone").attr("disabled", false);
    }
});
});

I want to change the color of input field when it is read only.

3
  • .css("background","red"); :) Commented Oct 15, 2014 at 19:44
  • How to change the color back to white when it it clicked male Commented Oct 15, 2014 at 19:49
  • just put one in each if() check, with the right bg color Commented Oct 15, 2014 at 19:51

5 Answers 5

17

UPDATED 22 Jul 2020


Method 1:

You can use : .css()

$("#age,#phone").css("background-color","#0F0");

$("input[name='sex']").change(function() {
    if($(this).val() == "female") {
        $("#age,#phone").attr("disabled", true).css("background-color","#0F0");
    } else if($(this).val() == "male") {
        $("#age,#phone").attr("disabled", false).css("background-color","#FFF");;
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="radio" name="sex" value="male">Male<br/>
<input type="radio" name="sex" value="female">Female <br/>
Age:<input type="text" name="age" id="age" size="20">
Hobbies:<input type="text" name="hobbies" id="hobbies" size="20"><br/>
Phone:<input type="text" name="phone" id="phone" size="20"><br/>


Method 2:

Another simple way can be using CSS class :

JS:

$("#age,#phone").addClass("green");

CSS:

.green { background-color:#0F0;}

$("input[name='sex']").change(function() {
                var isOptional = $(this).val() == "female";
        $("#age,#phone").attr("disabled", isOptional)
        if(isOptional) $("#age,#phone").addClass("green");
        else $("#age,#phone").removeClass("green");
});
.green{
    background-color:#0F0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="radio" name="sex" value="male">Male<br/>
<input type="radio" name="sex" value="female">Female <br/>
Age:<input type="text" name="age" id="age" size="20">
Hobbies:<input type="text" name="hobbies" id="hobbies" size="20"><br/>
Phone:<input type="text" name="phone" id="phone" size="20"><br/>


Method 3:

And there is yet another simple way using CSS selector to do so. :

JS:

$("#age,#phone").attr("disabled", true);

CSS:

input:disabled { background-color:#0F0;}

$("input[name='sex']").change(function() {
  $("#age,#phone").attr("disabled", $(this).val() == "female");
});
input:disabled {
  background-color: green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="radio" name="sex" value="male">Male<br/>
<input type="radio" name="sex" value="female">Female <br/>
Age:<input type="text" name="age" id="age" size="20">
Hobbies:<input type="text" name="hobbies" id="hobbies" size="20"><br/>
Phone:<input type="text" name="phone" id="phone" size="20"><br/>

Refer : https://developer.mozilla.org/en-US/docs/Web/CSS/:disabled

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

Comments

1
$("#age").css('background-color', 'green');

OR

$(this).css('background-color', 'green');

Comments

0

You can try this:

$("input[name='sex']").change(function () {
    if ($(this).val() == "female") {
        $("#age").attr("disabled", true);
        $("#age, #hobbies, #phone").css("background-color", "green");
        $("#phone").attr("disabled", true);
    } else if ($(this).val() == "male") {
        $("#age").attr("disabled", false);
        $("#phone").attr("disabled", false);
        $("#age, #hobbies, #phone").css("background-color", "white");
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="sex" value="male">Male
<br/>
<input type="radio" name="sex" value="female">Female
<br/>Age:
<input type="text" name="age" id="age" size="20">Hobbies:
<input type="text" name="hobbies" id="hobbies" size="20">
<br/>Phone:
<input type="text" name="phone" id="phone" size="20">
<br/>

Comments

0

Why dont you use just css?

Define a style for inputs disabled in your css file, and just disable them with js like you are doing right now...

For example:

input[disabled] { background-color:#F00; }

Comments

0

As a beginner this is my solution for changing the background color of an input field. I am not sure if this is the best practice, but I would like to share it with the experts here.

    $("#name").focusin(function() {
      $(this).addClass("green");
    });

    $("#name").blur(function() {
      $(this).removeClass("green");

      if (!$(this).val()) {
        $(this).addClass("warning");
      } else {
        $(this).removeClass("warning");
        $(this).addClass("green");
      }
    });
.warning {
  background-color: red;
  color: white;
}

.green {
  background-color: rgba(144, 238, 144, 1);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="formulier">
  <form>
    <div class="form-group row">
      <label class="col-md-2">Name: </label>
      <div class="col-md-6">
        <input type="text" class="col-md-6" id="name" />
      </div>
    </div>
  </form>
</div>

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.