the_critic
the_critic

Reputation: 12820

Find elements within a certain div and output their respective text with jQuery

I have created a little div .form_single_input_field which contains multiple .form_single_input_fields, whose respective text I would like to log. However, the alert will output all values concatenated together, while I would like to have an array of values. How do I achieve that ?

$(".form_single_input_field").keyup(function () {

    var actualTarget = $(this).parent();        
    alert(actualTarget.find('.form_single_selection_option').text());


});

I know that there is the .each() function, but this:

$(".form_single_input_field").keyup(function () {

    var actualTarget = $(this).parent();        
    $('.form_single_selection_option').each(function(){
         alert($(this).text());
    });


});

would alert all values from ALL .form_single_selection_options in my document, whereas I would like to have only the children and children's children of the selected/current field.

Upvotes: 0

Views: 45

Answers (1)

Maksim Gladkov
Maksim Gladkov

Reputation: 3079

Change your code to:

$(".form_single_input_field").keyup(function () {
    var actualTarget = $(this).parent();        
    $('.form_single_selection_option', actualTarget).each(function(){
         alert($(this).text());
    });
});

Upvotes: 2

Related Questions