Amanda_Panda
Amanda_Panda

Reputation: 1186

Retrieving value from a select tag in asp.net from controller to view

I am working on an asp.net webapp, and in a view I have a drop down list that the user can select a value from. The drop down list works just fine-the right text is displayed in the menu. But, when I try and use some basic JS to capture the value, I get "Uncaught TypeError: Object # has no method 'GetElementById' " in the JS Console in Chrome. Here is my code:

<select id="stop" onchange="sendInfo();">

@foreach(var blah in ViewBag.foobar)
{


<option value=@blah>@blah</option>

}
</select>

<script>
function sendInfo() {

    var stopId = document.GetElementById("stop").value;

}

</script>

Any help would be appreciated, I am very new to MVC and asp.net stuff.

Thanks,

Amanda

Upvotes: 2

Views: 998

Answers (3)

Artem Vyshniakov
Artem Vyshniakov

Reputation: 16465

You don't need to call getElementById function, you can access html element by event object which passed to each event handler:

<select id="stop" onchange="sendInfo(event);">

@foreach(var blah in ViewBag.foobar)
{
     <option value=@blah>@blah</option>
}
</select>

<script>
function sendInfo(event) {
        var stopId = event.target.value;
}

Upvotes: 1

nemesv
nemesv

Reputation: 139808

JavaScript is a case sensitive language and the method what your are looking for is getElementById

So you should write:

var stopId = document.getElementById("stop").value;

Upvotes: 1

Shmiddty
Shmiddty

Reputation: 13967

getElementById...

Case sensitivity matters.

Upvotes: 0

Related Questions