In my ASP.NET MVC application, I have a javascript file which has a jquery function in a javascript file under the Scripts folder. The function listens to drop down click event and fills the textbox with the selected value like below, but when I click the drop down option, this method doesn't get called and the value is not set in the text box. When I run with debugger (F12), if I paste the function in the debugger command line then clicking the drop down works!!!! Not sure why the method is not registered from the javascript file. The dropdown-menu and input text box are created using bootstrap input-group. The drop down is initially empty and filled by reading from a file in setup() function.
// In test.js
var testMap = {}
$(function () {
$("#dropdownlistId li a").click(function () {
var name = $(this).text();
$("#textBoxId").val(name);
});
});
function setup() {
// Read name and value pair in testMap
...
// Append names to drop down list
$.each(testMap, function (key, value) {
$("#dropdownlistId").append("<li> <a tabindex=\"-1\" href=\"#\"> " + key + "</a> </li>");
});
}
$(document).ready(function () {
setup();
});
In my cshtml file, I am calling the javascript file like:
@section Scripts {
@Scripts.Render("~/scripts/test.js")
}
<div class="col-md-3">
<div class="input-group">
<input id="textBoxId" type="text" class="form-control" placeholder="Enter name" />
<div class="input-group-btn">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul id="dropdownlistId" class="dropdown-menu pull-right"></ul>
</div>
</div>
</div>
$("#dropdownlistId li a").click()? Is your javascript file (test.js) getting loaded? When you view your developer tools, does the file showup in the dropdown of the "Script" tab?