I have 3 text boxs: Height, Weight, BMI and a Button called "Calculate" the JS function has to calculate the BMI of the person that clicks the button "Calculate", but it doesn't seem to work, I click the button and it does nothing, why is that? Do I have to create other class to put the JS code?
This is the edited view and the JS function :
@model IEnumerable<FitnessWebApplication.Models.Measurement>
@{
ViewBag.Title = "Index";
}
<script src="jquery.js"></script>
<script>
@section scripts{
$(document).ready(function () {
$('#calculate').click(function () {
var weight = Number($('#Weight').val());
var height = Number($('#Height').val());
var bmi = height / (weight * weight);
$('#BMIBox').val(bmi);
});
});//jq
}
</script>
<h2>Your Measurements</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Weight)
</th>
<th>
@Html.DisplayNameFor(model => model.Height)
</th>
<th>
@Html.DisplayNameFor(model => model.Neck)
</th>
<th>
@Html.DisplayNameFor(model => model.Waist)
</th>
<th>
@Html.DisplayNameFor(model => model.Taken)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Weight) kg
</td>
<td>
@Html.DisplayFor(modelItem => item.Height) cm
</td>
<td>
@Html.DisplayFor(modelItem => item.Neck) cm
</td>
<td>
@Html.DisplayFor(modelItem => item.Waist) cm
</td>
<th>
@Html.DisplayFor(modelItem => item.Taken)
</th>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
@Html.ActionLink("Details", "Details", new { id = item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.ID })
</td>
</tr>
}
</table>
<hr />
<h1>Calculate</h1>
<hr />
<h2>BMI</h2>
<form>
<table>
<tr>
<td align="right"><b>Weight:</b></td>
<td align="left"><div class="col-xs-5"><input type="text" name="Weight" class="form-control" id="Weight" /></div><b>kg</b></td>
</tr>
<tr>
<td align="right"><b>Height:</b></td>
<td align="left"><div class="col-xs-5"><input type="text" name="Height" class="form-control" id="Height" /></div><b>cm</b></td>
</tr>
<tr>
<td align="right"><b>BMI:</b></td>
<td align="left"><div class="col-xs-4"><input type="text" name="BMI" class="form-control" id="BMIBox" /></div></td>
</tr>
</table>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="button" value="Calculate" class="btn btn-default" id="calculate" />
</div>
</div>
</form>
<hr />
<h2>BF% (aproximated)</h2>
<p>(All the measurements must be taken at the narrowest point)</p>
<form>
<table>
<tr>
<td><b>Male</b></td>
<td><input type="radio" name="Male" /></td>
<td><b>Female</b></td>
<td><input type="radio" name="Female" /></td>
</tr>
<tr>
<td align="right"><b>Height:</b></td>
<td align="left"><div class="col-xs-5"><input type="text" name="Height" class="form-control" /></div><b>cm</b></td>
</tr>
<tr>
<td align="right"><b>Waist:</b></td>
<td align="left"><div class="col-xs-5"><input type="text" name="Waist" class="form-control" /></div><b>cm</b></td>
</tr>
<tr>
<td align="right"><b>Hip:</b></td>
<td align="left"><div class="col-xs-5"><input type="text" name="Hip" class="form-control" /></div><b>cm</b></td>
</tr>
<tr>
<td align="right"><b>Neck:</b></td>
<td align="left"><div class="col-xs-5"><input type="text" name="Neck" class="form-control" /></div><b>cm</b></td>
</tr>
<tr>
<td align="right"><b>BF%:</b></td>
<td align="left"><div class="col-xs-4"><input type="text" name="BF%" class="form-control" /></div><b>%</b></td>
</tr>
</table>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="button" value="Calculate" class="btn btn-default" onclick="location.href='<%: @Url.Action("CalculateBF", "Measurements") %>'">
</div>
</div>
</form>
$("#element").click(). This says "Find anything with an ID ofelement, and attach aclickevent to it." Pretty simple, right? But what if you tried to do that beforeelementwas even on the page? That's what your code is doing. You're not waiting for#calculateto load before you try and attach things to it. The selector$("#calculate")won't find anything on the page. Try wrapping it in a$(document).ready(function() { ... });instead.@section scripts{}needs to@RenderSection("scripts")to adding it to a special point of your view and you must use it only when your view uses aLayout. In a simple (without layout).cshtmlfile, you should use simple<script>tag.