I want to set a default value when my form is opened in my select.
I have a form that shares insert and modify functionality. So I have a parameter inside an if to know what action to perform and that is where I introduce my default value.
Form 1:
$('select option[value="0"]').attr("selected", true);
Form 2:
$("#selClaveR").val("Seleccionar");
But neither of those two makes my select put its value as selected.
Dearest, I leave you with two options, one using the latest JQuery library and the second using pure javascript, without the need to import anything.
With JQuery 3.2.0
Without any bookstore (old school)
Note that option zero, which is the one that says select an element, is in the middle, so if you remove the javascript, it won't be loaded by default at first. Greetings.
Well, just as you have it, just pick up the select and pass it a value and that's it.
In the js you must apply the change property after loading the val
This is a long time coming, but maybe someone else will still find it useful:
Let's say you have:
in the js what you have to do is:
I usually have this problem when I use some style library for my selects but I solve it by applying this.
Complementing this solution, it is very useful when you have a
<option>
value without value declared through the value attribute, to later restore it to its initial value in a form:(jquery) to select the first :