Reputation: 87
This is part of a lesson on Codecademy. This is a Jquery lesson. I cannot figure out how to append the .item
to the .list
. They don't explain it on the website and I have been unable to find an answer that I understand.
<!DOCTYPE html>
<html>
<head>
<title>To Do</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css"/>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<h2>To Do</h2>
<form name="checkListForm">
<input type="text" name="checkListItem"/>
</form>
<div id="button">Add!</div>
<br/>
<div class="list"></div>
</body>
</html>
$(document).ready(function() {
$('#button').click(function() {
var toAdd = $('input[name=checkListItem]').val();
$('div').append('<div class="item">' + toAdd+ '</div>');
});
});
The problem is that I cannot get .item
to add into .list
. I'm a brand new jQuery user so this is a bit difficult for me to understand. Even after fixing the syntax error it is not adding the correct items.
The website is giving me this error:Oops, try again. Make sure to use .append()
to add your .item
to your .list
!
Upvotes: 3
Views: 317
Reputation: 3348
try this:
$(document).ready(function() {
$('#button').click(function() {
var toAdd = $('input[name=checkListItem]').val();
$('.list').append('<div class="item">' + toAdd+ '</div>');
});
});
Upvotes: 1
Reputation: 67217
The that you have used will not append the elements as you expected.
Try to pass the class and text as an object,
$(document).ready(function() {
$('#button').click(function() {
var toAdd = $('input[name=checkListItem]').val();
$('div.list').append($('<div>',{"class":"item","text":toAdd}));
});
});
Upvotes: 0
Reputation: 31749
$('#button').click(function() {
var toAdd = $('input[name=checkListItem]').val();
$('div').append('<div class=".item">') + toAdd+ '</div>'; // Error on this line
});
You are closing function incorrectly. It should be -
$('div').append('<div class="item">' + toAdd+ '</div>');
Upvotes: 4