For a custom framework like Bootstrap select https://silviomoreto.github.io/bootstrap-select/ how do I change the border color of a select box in Javascript?
I tried this but does not work.
document.getElementById("box").style.borderColor = "red";
Javascript
function validate() {
var ok = true;
var box = document.getElementById("box").value;
if (!box) {
document.getElementById("box").style.borderColor = "red";
ok = false;
}
return ok;
}
HTML
<form action="#" onsubmit="return validate()" method="POST">
<select id="box" name="num" class="selectpicker form-control">
<option value="" selected="selected">select number</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<div class="text-center">
<button type="submit" class="btn btn-primary">submit</button>
</div>
</form>
You can override the CSS
Hope this helps
Update: By using JavaScript, this can be done as shown below (considering all required JS file as added already)
If you see the DOM element generated by
bootstrap-select
, it actually hides the originalselect
element and creates its own structure withdiv
,span
andbuttons
. There isn't any official documentation provided to change itsborder
, but as a workaround you could do it as below:You need to change border of the
button
whoseclass
isbtn dropdown-toggle btn-default
, which is generated bybootstrap-select
. Since the element created will not be given anyid
we will make use of itsclass
withdocument.getElementsByClassName("classnames")[0]
Update
Explanation in comments.