Nathan McKaskle Nathan McKaskle - 9 months ago 41
jQuery Question

('...').each not a valid function for asp.net core mvc with jquery bundle

Reposting this because I cannot find any answer that doesn't involve using Google's bundle or something else. Please tell me if I'm missing any detail so I'll add it. I have the whole jquery bundle included with VS referenced at the top of my _Layout...

@Scripts.Render("~/bundles/jquery")


Here's the BundleConfig.cs

public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));

bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.validate*"));

// Use the development version of Modernizr to develop with and learn from. Then, when you're
// ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
"~/Scripts/modernizr-*"));

bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
"~/Scripts/bootstrap.js",
"~/Scripts/respond.js"));

bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/bootstrap.css",
"~/Content/site.css"));
}


...but it is still telling me in this case that
.each
is not a function. What I'm missing or need to refer to the layout or BundleConfig? Why isn't
.each
a function that is a part of the jquery bundle that comes with Visual Studio 2017?

@Html.ListBox("Groups", null, new { size = 10, style = "width : 350px; max-width : 500px;", id = "grouplist" })


The whole context of my jquery:

$('#savegroups').click(function () {
var groupId = $('#groupId').val();
var list = [];
('#grouplist').each(function (item) {
list.append('{"groupId":"' + groupId + '","groupName":"' + item.value +'"}');
});
$.ajax({
type: "POST",
url: "/GroupCategories/EditGroups",
data: JSON.stringify(list),
});
});


Error:

1:230 Uncaught TypeError: "#grouplist".each is not a function
at HTMLInputElement.<anonymous> (1:230)
at HTMLInputElement.dispatch (jquery:1)
at HTMLInputElement.y.handle (jquery:1)

Answer Source

Looks like you are missing the jquery selector..

('#grouplist')

should be

$('#grouplist option')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download