Abdul Muqeet Abdul Muqeet - 1 month ago 15
Javascript Question

Iterate to div to get different input field and post it to controller

I have a main div that have class models and in this div I have different input type I want to iterate through main div with class models and get the the different input fields and post it to my controller but end up with this error. ReferenceError: getElementsByClassName is not defined

$("#Visitor-form").on("submit", function () {
//Code: Action (like ajax...)
var slides = getElementsByClassName("models");
console.log(slides);
for (i = 0; i < slides.length; i++) {

var previsitor = [
{ NIC: slides[0].value, V_Name: slides[0].value }
];
}
previsitors = JSON.stringify({ 'things': previsitor });

$.ajax({
contentType: 'application/json; charset=utf-8',
dataType: 'json',
type: 'POST',
url: '/PreVisitor/addMultipleVisitor',
data: things,
success: function () {
$('#result').html('"Pass List<PreVisitor>" successfully called.');
},
failure: function (response) {
$('#result').html(response);
}
});

})

Answer

if you use jquery stick with jquery

 var slides = $(".models");

you forgot the document object in your current syntax