mercuryyy mercuryyy - 3 years ago 157
Javascript Question

alphabetically sort an array of inputted words with javascript/jquery

I am new to javascript and jquery. I need help figuring out what is wrong with my code because it is not executing ".sort()" and ".join(", ") during the last steps...

I want the page to have a pop-up box where the user types in a place (inputs are stored in the array) and continues that until they type 'done', and then the page loads their list of places in alphabetical order with commas. I got the first part ok (the prompts and entering text), but my page doesn't load the entered material after entering 'done'. Below is my code:

<div id="outputPlaces"></div>

<script>
$(document).ready(function() {
var favPlaces = [];
var input = prompt("Please enter your favorite place or type done to stop entering places.");
while (input != 'done') {
favPlaces.push(input);
input = prompt("Please enter another favorite place or type done to stop entering places.");
}
favPlaces.sort();
$('#outputPlaces').html = favPlaces.join(", ")

});

</script>

Answer Source

You can't set the HTML using assignment. The html function gets the HTML in the element. What you want is .text:

.text(favWords.join(", "))

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download