I have a problem with javascript, this is a little part of my MVC view page.
<body>
...
<form>
<div class="form-group, row">
<div class="col-sm-2">
<label class="control-label2" for="comparison">Compare:</label>
</div>
<div class="col-sm-2">
<select class="form-control" id="comparison" name="comparison">
@*<option></option>*@
<option value="<="><=</option>
<option value=">=">>=</option>
<option value="<"><</option>
<option value=">">></option>
<option value="=" selected>=</option>
</select>
</div>
<div class="col-sm-3">
@Html.ValidationMessageFor(Function(model) model.comparison)
</div>
</div>
...
</form>
</body>
After the page loads I need to modify selected option under some condition. My problem is: if before body tag I insert a button with onClick clause that calls
function myFunc(){
document.getElementById("comparison").value = "<";
}
everything works. If I insert the button and myFunc
inside the form tag, nothing works.
I would like to automate the process with condition after page loading. Also I want to change background color of dropdown if other conditions are not satisfied.
2 Answers 2
Please use below events based on your scenario. Inside this events, you can change dropdown value based on condition.
When the page loads totally (dom, images, ...)
$(window).load(function(){
// full load
});
or
$(document).load(function () {
// code here
});
After DOM load (not wait for all images to loaded)
$(function(){
// DOM Ready
});
Comments
SOLVED!
When I installed jquery library with NuGet installer, Visual Studio included the wrong script string in _Layout.vbhtml.
It inserted <script src="~/script/jquery-1.10.2.js">
instead of <script src="~/script/jquery-3.2.1.js">
.
btn
&myFunc
inside form tag'? Are you want to triggermyFunc
from certain form event? Please explain further what you need to do with example above.btn
&myFunc
in form tag. I want, after loading page, automate selection of "default" value and set the background color of dropdown by condition. Sorry, i badly explained myself$(document).ready
or$(window).load
. But, when executing one of them, the script doesn't start at all. I need to call only this linedocument.getElementById("comparison").value = "<";
after page loading.