Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
melissa brown
4,670 Pointsplace input from a user somewhere else on the page
hi im trying to build a shopping list where the data from the input is placed in an unordered list below but i cant get it to work? also having problems with the css. the unordered list keeps collapsing into the add and remove buttons section.http://codepen.io/mbrwn/pen/QbePRd
3 Answers
Brian Steele
23,060 PointsHi there!
The issue with alignment was because you were missing a closing div tag (see below for correct html)
Also, it looks like you were missing a few correct selectors in your JS (specifically the '#' before add_button. Below is a solution that works.
<body>
<div class="form-div">
<form class="form-content">
<h1>Shopping List</h1>
<input type="text" name="shop_ListItem">
<div id="add_button" class="icon">
<i class="fa fa-plus fa-2x"></i>
</div>
<div id="remove" class="icon">
<i class="fa fa-minus fa-2x"></i>
</div>
<div class="sortable_list">
<ul>
<li>test</li>
</ul>
</div>
</form>
</div>
</body>
$(document).ready(function() {
$("#add_button").click(function() {
var toAdd = $("input");
$('ul').append("<li>"+toAdd.val()+"</li>");
toAdd.val(""); //clearing out the input on add
});
});
melissa brown
4,670 Pointsthank you i was spending hours on it. So i understand the syntax better. as a general rule you dont need to name the input with a class or id for the variable $("input[name=shop_ListItem]") like how i did before?
clearly im not understanding how to correctly use the value attribute. so from your code the rule is like this $(selector).val ( --value goes in here-- its empty so the user can fill it in--) and we cant just declare a variable like i did before "<li>"+toAdd.+"</li>" we have to say what we can from it eg the value of the variable toAdd.val()?
i dont really understand how this section works toAdd.val(""); //clearing out the input on add it clears out the data from the input section but how?
Brian Steele
23,060 PointsNot necessarily. Your selector for the input element was fine (basically, any CSS selector will work inside the parens. If you add that back in, it should still work-in your example you only had one input on your page, hence, the additional specificity was unecessary. However, in a real world page, you'd probably have multiple inputs, and if you wanted to only select one of said inputs, you'd need to use the selector to focus in on that one (much how you did). The thing keeping your code from working was the missing '#' in front of 'add_button'.
In terms of what I wrote, here's the plain text version: 1) When the DOM loads 2) Select the element with the ID of '#add_button' 3) When the user clicks the above element 4) Run the anonymous function 5) Grab the current value of the input and store it in the variable 'toAdd' 6) Select the 'ul' element 7) Append the value of the variable 'toAdd' to the end of the 'ul' as a new 'li' 8) Clear out the value of the input 9) Close the anonymous function
Hope that helps. Keep plugging away, when JS 'clicks' for you, it's a lot of fun!
melissa brown
4,670 Pointsthank you
Brian Steele
23,060 PointsBrian Steele
23,060 PointsHere is the working pen for reference. Cheers!