Mike Hemeles Posted September 22, 2017 Share Posted September 22, 2017 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 More sharing options...
dsonesuk Posted September 22, 2017 Share Posted September 22, 2017 (edited) 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 September 22, 2017 by dsonesuk 1 Link to comment Share on other sites More sharing options...
Mike Hemeles Posted September 22, 2017 Author Share Posted September 22, 2017 (edited) Thanks for your response, It's works perfectly, return (value !== ""); //it was for the end of the loop, but I think it was necessary in fact Thanks, And, is there a way to put an input for each label? Edited September 22, 2017 by Mike Hemeles Link to comment Share on other sites More sharing options...
dsonesuk Posted September 22, 2017 Share Posted September 22, 2017 You would have to use document.createElement('input'); then assign necessary attributes to it, then append it, OR create input as you normally would in html as a string, and append that. Link to comment Share on other sites More sharing options...
shree420 Posted November 29, 2017 Share Posted November 29, 2017 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 More sharing options...
dsonesuk Posted November 29, 2017 Share Posted November 29, 2017 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now