Home » Jquery » javascript – how to pass id of a multiple dropdown inside the $.each function

javascript – how to pass id of a multiple dropdown inside the $.each function

Posted by: admin February 22, 2020 Leave a comment

Questions:
function fieldsFilter(flag,value,chkClass)
	{
		var checkedfields = [];
		
		$.each($("#rep_id option:selected"), function(){            
            checkedfields.push($(this).val());
        });
		$('#'+flag+'Filters').val(checkedfields);
	}

Here i have a javascript function. Here i want to pass the chkClass in the place of rep_id in $.each(). i
am getting confusion.can one please help me

How to&Answer:

Try this:

function fieldsFilter(flag,value,chkClass) {
    var checkedfields = [];
    $.each($(`#${rep_id} option:selected`), function(){
        checkedfields.push($(this).val());
    });
    $('#'+flag+'Filters').val(checkedfields);
}