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

jQuery – increment inside .each() function not working

I’m trying to add an unique id attribute with incremented value number to all radio buttons and also for attribute to all labels. This is not working:

$(document).ready(function() {
    var i = 0;

    $('.wpcf7-radio .wpcf7-list-item').each(function() {
        i++;

        $('.wpcf7-radio .wpcf7-list-item input').attr('id', 'radio-' + i);
        $('.wpcf7-radio .wpcf7-list-item .wpcf7-list-item-label').attr('for', 'radio-' + i);                    
    });
});

This is output HTML:

<span class="wpcf7-form-control wpcf7-radio">
    <span class="wpcf7-list-item first">
        <input type="radio" name="moznost-typ-projektu" value="Nový projekt" checked="checked" id="radio-5" />
        <label class="wpcf7-list-item-label" for="radio-5">Nový projekt</label>
    </span>

    <span class="wpcf7-list-item">
        <input type="radio" name="moznost-typ-projektu" value="Prerábka / redizajn existujúceho projektu" id="radio-5" />
        <label class="wpcf7-list-item-label" for="radio-5">Redizajn</label>
    </span>

    <span class="wpcf7-list-item last">
        <input type="radio" name="moznost-typ-projektu" value="Niečo iné" id="radio-5" />
        <label class="wpcf7-list-item-label" for="radio-5">Niečo iné</label>
    </span>
</span>

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 try it :

$(document).ready(function() {
    var i = 0;

    $('.wpcf7-radio .wpcf7-list-item').each(function() {
        i++;
        $(this).find('input').attr('id', 'radio-' + i);
        $(this).find('label').attr('for', 'radio-' + i);                    
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span class="wpcf7-form-control wpcf7-radio">
    <span class="wpcf7-list-item first">
        <input type="radio" name="moznost-typ-projektu" value="Nový projekt" checked="checked" id="radio-5" />
        <label class="wpcf7-list-item-label" for="radio-5">Nový projekt</label>
    </span>

    <span class="wpcf7-list-item">
        <input type="radio" name="moznost-typ-projektu" value="Prerábka / redizajn existujúceho projektu" id="radio-5" />
        <label class="wpcf7-list-item-label" for="radio-5">Redizajn</label>
    </span>

    <span class="wpcf7-list-item last">
        <input type="radio" name="moznost-typ-projektu" value="Niečo iné" id="radio-5" />
        <label class="wpcf7-list-item-label" for="radio-5">Niečo iné</label>
    </span>
</span>

in your code, in every loop, you changed id and for of all elements

.wpcf7-radio .wpcf7-list-item input is 3 elements input

.wpcf7-radio .wpcf7-list-item label is 3 elements label

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