Tenebor
Tenebor

Reputation: 1

javascript in MVC view doesn't work

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.

Upvotes: 0

Views: 231

Answers (2)

Tenebor
Tenebor

Reputation: 1

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

Upvotes: 0

thiyagu selvaraj
thiyagu selvaraj

Reputation: 100

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
});

Upvotes: 1

Related Questions