houman_ag houman_ag - 5 months ago 32
Javascript Question

Accessing a view model element through Javascript

I am passing a Model to my view that is called HomeViewModel. The HomeViewModel has a bool property called isNext. In my view, I simply want to display the isNext value. So basically I have this:

@model Impoware.Models.HomeViewModel

ViewBag.Title = "View Contest";

<h2>View Current Contest</h2>

@using (Html.BeginForm())
<td>@Html.LabelFor(m => m.isNext)</td>
<td align="right">Submenu goes here</td>

In the same view, I have a simple div that onclick calls setIsNext(true). Essentially I want to set the value of isNext to true/false based on what is clicked but seemingly javascript cannot find the elemntbyId('isNext') (alert('2') never gets called). Any ideas?

var setIsNext = function(input){
var output = document.getElementById('isNext');

if (output != null) {
output.innerHTML = input;


You will need to specify ID in the View file

// in cshtml

@Html.LabelFor(m => m.isNext, new { id = "MyNextId" })

var output = document.getElementById('MyNextId');