Jump to content

jQuery each function


Mike Hemeles

Recommended Posts

Hello,

$(function () {
    $('#selectliste').change(function () {
        var l = selectliste.options[selectliste.selectedIndex].innerHTML;
        var tous = l.split(' ');
        $.each(tous, function(index, value) {
            $("#label").html(value) + "<br>";

            return ( value !== "" );
        });
    })
});

here, I want to get the value of my select box which are in string type, after I explode the string in array, the set the value of array in label,
But, all worked wonderfully until 

$("#label").html(value) + "<br>";

 

which only retrieves the last list of my array.
How can I make to recover all the value of my array, thank you!

Link to comment
Share on other sites

You are not getting the value, but text content of option list?

Every time the loop completes a cycle it overrides the previous html, you need to add to previous, then at the end add to label html

var join = "";
                    $.each(tous, function(index, value) {
                        join += value + "<br>";
                        //return (value !== ""); ///// why?
                    });
                    $("#label").html(join);

I think this is what you are asking for?

As I said, you are using innerHTML not value, so you need to adjust this to your requirements.

instead of

 var l = selectliste.options[selectliste.selectedIndex].innerHTML;

you could use

var l = $(this).val(); // for value

or

var l = $(this).text(); // for actual text description

 

Edited by dsonesuk
  • Like 1
Link to comment
Share on other sites

  • 2 months later...

When you are putting return inside the jQuery Each method then this is not letting the loop to run for every item. Simply remove this line. The final jQuery each code will be:

 

$(function () {
    $('#selectliste').change(function () {
        var l = selectliste.options[selectliste.selectedIndex].innerHTML;
        var tous = l.split(' ');
        $.each(tous, function(index, value) {
            $("#label").html(value) + "<br>";
            //return ( value !== "" );
        });
    })
});

 

Link to comment
Share on other sites

4 hours ago, shree420 said:

When you are putting return inside the jQuery Each method then this is not letting the loop to run for every item. Simply remove this line. The final jQuery each code will be:

 


$(function () {
    $('#selectliste').change(function () {
        var l = selectliste.options[selectliste.selectedIndex].innerHTML;
        var tous = l.split(' ');
        $.each(tous, function(index, value) {
            $("#label").html(value) + "<br>";
            //return ( value !== "" );
        });
    })
});

 

Which again! will result in showing last value in array, after overwriting all the previous.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...