UserX
UserX

Reputation: 525

JavaScript function not being called from ASP.NET MVC HTML view

I'm new to ASP.NET MVC and I'm just trying to call a JavaScript function to record a user's listbox selection. I have the JavaScript function duplicated in THREE PLACES, but it still can't be found. What am I doing wrong?

Here's the code for the view. The function I'm trying to call is JobSelected, in the onselect attribute:

@model IList<Nexient.Net.Orgchart.Data.Models.JobTitle>

@{
    ViewBag.Title = "Delete";
}

<h2>Delete</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <head>
        <title>the title</title>
        <script type="text/javascript">
            function JobSelected() {
                alert("In JavaScript!");
            }
        </script>
    </head>

    <select multiple="multiple" size="10" onselect="JobSelected();" id="JobList">
        @foreach (var jobTitle in Model)
        {
            <option>
                @jobTitle.Description
            </option>
        }
    </select>
    <fieldset>
        <legend>Please select job title(s) to delete.</legend>

        <p>
            <input type="submit" value="Delete"/>
        </p>
    </fieldset>

}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

<script type="text/javascript">
    function JobSelected() {
        alert("In JavaScript!");
    }
</script>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")

<script type="text/javascript">
    function JobSelected() {
        alert("In JavaScript!");
    }
</script>
}

Upvotes: 1

Views: 630

Answers (1)

Sabre
Sabre

Reputation: 359

The onselect event is triggered when text is selected (highlighted) in a control. You probably meant to use onchange instead, which is triggered when the value of the control changes (such as by selecting a different option in the select control).

Upvotes: 5

Related Questions