I want to change the select component value on page load. I read the related post and every solution presented there works for a button click. However, what I would like to have, is value set after the page is loaded, without any user action. What I have is:
<script>
$(document).ready(function(){
$("div.id_100").val("March").change();
alert("ready!");
});
</script>
and:
<div class="id_100">
<select class="form-control">
<option th:text="January">January</option>
<option th:text="February">February</option>
<option th:text="March">March</option>
</select>
</div>
I have the same piece of code attached to a button, and it works. It just doesn't work for ready() function. So my question is: how to set select value after the page is loaded?
$( "#button1" ).click(function() {
$("div.id_100").val("March").change();
});
Full html
<head>
<title>Wszystkie Delegacje</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="../static/css/blog.css" th:href="@{/css/blog.css}" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.13/css/dataTables.bootstrap.min.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.13/js/dataTables.bootstrap.min.js"></script>
<script th:src="@{/js/triptable.js}"></script>
<link
href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.0/bootstrap-table.min.css"
rel="stylesheet" />
<style type="text/css">
.pull-right {
float: right !important;
color: green;
}
@media ( min-width : 1600px) {
.container {
max-width: 2000px;
}
}
</style>
</head>
<body>
<div layout:fragment="content">
<div class="container">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.0/bootstrap-table.min.js"></script>
<a class="btn btn-md btn-info" id="button1">miech</a>
<div class="id_100">
<select class="form-control">
<option th:text="January">January</option>
<option th:text="February">February</option>
<option th:text="March">March</option>
</select>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script>
$(document).ready(function(){
$("div.id_100 select").val("March").change();
alert("ready!");
});
$( "#button1" ).click(function() {
$("div.id_100").val("March").change();
});
</script>
</body>
</html>