0

Thanks in advance.

I want to change the value of input text "total" in a form depending the value of the select "opciones". I tried with onchange(), with document.getElementById("").value but it doesn't works.

I dont know what is failing, but I cannot change the input value.

    <form name="formulario">

        <select name="opciones" id="opciones">
                                        <option>1</option>
                                        <option>2</option>
                                        <option>3</option>
                                    </select>
                  <input type="text" name="suma" id="total">
</form>

Javascript:

    function formtotal() {

    if (document.formulario.opciones.value = "1") {

        document.formulario.suma.value = "1000";
    }

    else if (document.formulario.opciones.value = "2") {

        document.formulario.suma.value = "1250";
    }

    else if (document.formulario.opciones.value = "3") {

        document.formulario.suma.value = "1500";
    }

}

3 Answers 3

2

I think you should try to work with

let e = document.getElementById("opciones");
let total = document.getElementById("total");

switch(e.selectedIndex) {
case 0:
    total.value = 1000;
    break;
case 1:
    total.value = 1250;
    break;
case 2:
    total.value = 1500;
    break;
default:
    total.value = 0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for using a switch statement, it's a much cleaner approach and allows you to handle the default case for when some idiot goes and edits the drop-down without updating the Javascript.
0

I think instead of document.formulario.suma.value it should be document.formulario.total.value.

However, below code is working fine. Tested in jsfiddle (link below).

function formtotal() {
    var x = document.getElementById("opciones").value;
    if (x == "1") {
        document.getElementById("total").value = "1000";
    }
    else if (x == "2") {
        document.getElementById("total").value = "1250";
    }
    else if (x == "3") {
        document.getElementById("total").value = "1500";
    }
}

Add onchange to the select tag as per below snippet:

<select name="opciones" id="opciones" onchange="formtotal()">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>

https://jsfiddle.net/fghxhtmk/ - Working fine. Please check.

EDIT: Added "value" to each option tag.

4 Comments

Could you please visit the link provided : jsfiddle.net/fghxhtmk
Hi @PabloBarrios , I missed out "value" attribute in <option> tag. Please check my answer again. It will work fine this time. :)
Thanks, now its working. I tried to refresh cache with all of your recommendations. ;)
You're welcome :) and thanks for marking my answer correct. :)
0

One clear mistake I can see is your use of the equality operator document.formulario.opciones.value = "1"

Should be

document.formulario.opciones.value == "1"

Due to this, your first if statement is always going to be evaluated as true.

1 Comment

I tried it changing = for == and it doesnt works too

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.