Running Rabbit
Running Rabbit

Reputation: 2720

.each not working in jquery

I am trying to iterate over some values of a dropdown, but I guess my .each is not working for me. Below is the dropdown values through which I am trying to iterate.

.cshtml

<select class="hqSelect"> 
<option value="-1">-Select HQ/NCM Company HQ</option> 
<option value="142167">1-2-1 CLAIMS INC.</option> 
<option value="183421">1-2-1 CLAIMS():-/</option> 
<option value="154668">FARMERS INSURANCE</option> 
<option value="183281">GLOBAL LOGIC INC</option> 
<option value="183488">HQQQQQ</option> 
<option value="183282">IYOGI</option> 
<option selected="selected" value="183423">MY HQ 1</option> 
<option value="183287">NOORHQRIYA</option> 
<option value="183285">PRITIHQ</option> 
<option value="183402">PT_COMPASSRIYA</option> 
<option value="183296">RATNESHHQQ</option> 
<option value="183300">TEST_ASSOCIATION_HQ</option>
</select>   

Now on my js, I am trying to use .each to iterate to the select value of the class .hqSelect so that I can change the selected value based on my logic. But my .each is not working. Below is the .js code

$("#btnSearchPopup").on("click", function (e) {
        var hqcheckedVal = false;
        var companyId;
        var branchCheckedVal = [];
        var entityType = $("input[id*='hdnEntityType']").val();
        var temp = '183488';
        $('input[name="subscribe"]').each(function () {
            if (this.checked) {
                hqcheckedVal = this.checked;
                companyId = $(this).closest('tr').find('#headquarterIdForSearch').text();
                var companyName = $(this).closest('tr').find('#companyNameId').text();
                //$(this).parents().find('.hqSelect option:selected').val(companyId);

                //var curText = $(this).parents().find('.hqSelect option:selected').text();
                //$(this).parents().find('.hqSelect option:selected').text().replace(curText, companyName);
            }
        });

$('.hqSelect').each(function () {
                    if (this.value === companyId) {
                        this.attr("selected", "selected");
                        return;
                    }
                });
    });

the .hqSelect has select values when I see at run time, but still I can't iterate through them.

Is it something that I am missing.

Upvotes: 1

Views: 806

Answers (2)

Mairaj Ahmad
Mairaj Ahmad

Reputation: 14624

You can simply check if select contains companyid select that value.

if($('.hqSelect option[value="' + companyId+ '"]').length > 0)
  $('.hqSelect').val(companyId); 

Upvotes: 0

Suren Srapyan
Suren Srapyan

Reputation: 68685

Because you query for only the elements with the class hqSelect which is a single item.You need to get the options inside the select.Change your code to

$('.hqSelect option').each(function () {
                    if ($(this).val() === companyId) {
                        $(this).prop("selected", true);
                        return;
                    }
                });
    });

Upvotes: 4

Related Questions