JHUK
JHUK

Reputation: 11

Javascript onchange event

Im trying to use Javascript to change the background color of a text input depending upon a value selected in a drop down box.

I want the background color of "businessname" input to be changed to yellow if "Business" is selected from the dropdown.

This is the HTML for the dropdown

<select id="type" name="type" onchange="individualOrBusiness()">
    <option value="select">Select</option>
    <option value="business">Business</option>
    <option value="individual">Individual</option>
</select>

The input I wish to change

<input type="text" id="businessname" name="businessname">

and the Javascript I have tried

function individualOrBusiness() {
    var x = document.getElementById("type").value;
    if (x == "Business") {
        document.getElementById("businessname").style.backgroundColor = "yellow";
    }
}

I've tried a few variations on the above however I cant get it to work, if anyone point me in the right direction?

Cheers

Upvotes: 0

Views: 3008

Answers (1)

Quentin
Quentin

Reputation: 943108

If in doubt look at your data.

alert(x);

Your value will never be "Business".

<option value="business">Business</option>
               ^^^^^^^^

It is "business" with a lowercase B.

Upvotes: 3

Related Questions