0

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.

asked Nov 17, 2017 at 8:36
5
  • What do you want by 'insert btn & myFunc inside form tag'? Are you want to trigger myFunc from certain form event? Please explain further what you need to do with example above. Commented Nov 17, 2017 at 8:42
  • I don't want insert 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 Commented Nov 17, 2017 at 11:51
  • So your script does work, it's just that you don't know how to do it when the page finish loading? Commented Nov 17, 2017 at 12:24
  • I know how to start my script when the page finishes loading, i can use $(document).ready or $(window).load. But, when executing one of them, the script doesn't start at all. I need to call only this line document.getElementById("comparison").value = "<"; after page loading. Commented Nov 17, 2017 at 12:48
  • Ok so "I know how to start my script ..." but then also "the script doesn't start at all"... Which is it? Commented Nov 17, 2017 at 21:43

2 Answers 2

1

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
});
Flexo - Save the data dump
89.1k22 gold badges205 silver badges286 bronze badges
answered Nov 19, 2017 at 18:51

Comments

0

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">.

answered Nov 24, 2017 at 11:21

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.