0

I have 2 comboboxes and I want to select a specific item by specifying a value using JS but they don't seem to work.

these are the Comboboxes:

                    <div class="row">

                        <div class="col-sm-6 form-group">
                            <label for="email"> Estado:</label>
                            <select class="form-control" id="Estado" name="cbmEstado" required> 
                            <option value="" selected="selected"></option>
                            <option value="1">Activo</option>
                            <option value="0">Inactivo</option>
                            </select>
                        </div>
                        <div class="col-sm-6 form-group">
                            <label for="email"> Exonerado:</label>
                            <select class="form-control" id="Exonerado" name="cbmExonerado" required> 
                            <option value="" selected="selected"></option>
                            <option value="1">Sí</option>
                            <option value="0">No</option>
                            </select>
                        </div>
                    </div>

this is the code to select the item in the Combobox:

 <script type="text/javascript">
    window.onload = function() {
    document.getElementById('Exonerado').value =1;
    document.getElementById('Estado').value =1;
}
</script>
1
  • 1
    Try putting the 1 in quotes .value = "1" Commented Mar 7, 2019 at 14:34

3 Answers 3

4

Chrome seems to do exactly what you wanted. But I suspect that the real problem here is that you pass a Number and not a String.

If you wrap them in quotes like in the following code snippet, it works like a charm.

window.onload = function() {
  document.getElementById('Exonerado').value = "1";
  document.getElementById('Estado').value = "0";
}
<div class="row">

  <div class="col-sm-6 form-group">
    <label for="email"> Estado:</label>
    <select class="form-control" id="Estado" name="cbmEstado" required>
      <option value="" selected="selected"></option>
      <option value="1">Activo</option>
      <option value="0">Inactivo</option>
    </select>
  </div>
  <div class="col-sm-6 form-group">
    <label for="email"> Exonerado:</label>
    <select class="form-control" id="Exonerado" name="cbmExonerado" required>
      <option value="" selected="selected"></option>
      <option value="1">Sí</option>
      <option value="0">No</option>
    </select>
  </div>
</div>

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

Comments

2

window.onload = function() {
  document.getElementById('Exonerado').value = "1";
  document.getElementById('Estado').value = "1";
}
<div class="row">

  <div class="col-sm-6 form-group">
    <label for="email"> Estado:</label>
    <select class="form-control" id="Estado" name="cbmEstado" required>
      <option value="" selected="selected"></option>
      <option value="1">Activo</option>
      <option value="0">Inactivo</option>
    </select>
  </div>
  <div class="col-sm-6 form-group">
    <label for="email"> Exonerado:</label>
    <select class="form-control" id="Exonerado" name="cbmExonerado" required>
      <option value="" selected="selected"></option>
      <option value="1">Sí</option>
      <option value="0">No</option>
    </select>
  </div>
</div>

Now this will help you out from your query

Comments

0

There is no problem in the code so far, it should work without any problem.

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.