1

I want to create an auto-set of values in input or select tag based on the condition provided in the java script below: My problem here is that whenever i submit the data i wont reflect in the database. Any idea or a snippets of code would a great help.

function myFunction()
{
    var condition = document.getElementById("course").value;
    var text

    if(condition==="BSCS"|| condition==="BSIT"|| condition==="BSIS"||condition==="BLIS"||condition==="BSEMC")
    {
        text="Department A";
    }
    else if(condition==="BSA"||condition==="BSBA"||condition==="BSOA"||condition==="BSREM"||condition==="BSHRM"){
        text="Department B";
    }else if(condition==="BEED"||condition==="BSED"||condition==="BSSW"||condition==="AB-PolScie"||condition==="AB-Philo"||condition==="AB-English"){
        text="Department C";
    }else{
        text="N/A";
    }
    document.getElementById("demo").innerHTML=text;
}
<select id="course" onchange="myFunction()" class="form-control" 
name="Course">
                <option value="None">Select Course</option>
                <option value="BSCS">BSCS</option>
                <option value="BSIT">BSIT</option>
</select>

enter image description here

1 Answer 1

3

You heve used innerHTML which is wrong if you want to set a value, use value.

document.getElementById("demo").innerHTML=text;

change to:

document.getElementById("demo").value=text;

function myFunction()
{
    var condition = document.getElementById("course").value;
    var text

    if(condition==="BSCS"|| condition==="BSIT"|| condition==="BSIS"||condition==="BLIS"||condition==="BSEMC")
    {
        text="Department A";
    }
    else if(condition==="BSA"||condition==="BSBA"||condition==="BSOA"||condition==="BSREM"||condition==="BSHRM"){
        text="Department B";
    }else if(condition==="BEED"||condition==="BSED"||condition==="BSSW"||condition==="AB-PolScie"||condition==="AB-Philo"||condition==="AB-English"){
        text="Department C";
    }else{
        text="N/A";
    }
    document.getElementById("demo").value=text;
}
<select id="course" onchange="myFunction()" class="form-control" 
name="Course">
  <option value="None">Select Course</option>
  <option value="BSCS">BSCS</option>
  <option value="BSIT">BSIT</option>
</select>

<input id="demo" type="text" name="Dept" class="form-control" placeholder="Department" required>

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

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.