Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting an error when i loop through to get input name value

Can’t figure out why this loop won’t give me the info i’m wanting

I have a page with a few hundred inputs on it , i want to get the the input "name" value for each input that is checked

<input type="checkbox" name="TAXI_SQUAD0001" checked="checked">
<input type="checkbox" name="TAXI_SQUAD0021" checked="checked">
<input type="checkbox" name="TAXI_SQUAD0011">   


$.ajax({
    url: urlHPM,
    xhrFields: {
        withCredentials: true
    },
    cache: false,
    type: "GET",
    success: function (data) {
        checkedInputs = $(data).find('input[checked="checked"]'); 
        for (var i = 0; i < checkedInputs.length; i++) {
            console.log(checkedInputs.attr('name')[i]); // loops character of first name of first input over and over
            console.log(checkedInputs[i].attr('name')); // error attr not a function
            // i want to log input names TAXI_SQUAD0001 and TAXISQUAD0021
        }

    },
    error: function (xhr) {
    }
});

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can loop all checked checkboxes in the ajax success response using the .each() jQuery function.

$.ajax({
    url: urlHPM,
    xhrFields: {
        withCredentials: true
    },
    cache: false,
    type: "GET",
    success: function (data) {
        checkedInputs = $(data).find('input[checked="checked"]');
        $(checkedInputs).each(function(){
            //Log the input name attribute
            console.log($(this).attr('name'));
        });
    },
    error: function (xhr) {
    }
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading