12. Append To Body - Code passes but getting 'undefined'


Getting 'undefined' in red, code passes but doesn't add to the list. Help?!

$(document).ready(function() {
    $('#button').click(function() {
    var toAdd = $('input[checkListItem]').val();
    $('.list').append('<div class="item">' + toAdd + '</div>');


I don't see anything amiss in your code, except for one thing:


We need to include the attribute name with its value.


The above is how it appears in the HTML.


Attempted to try and change the format of 'checkListItem' to include both single and double quotes, neither seemed to fix the 'undefined' error i'm getting. :frowning:


Did you correct syntax in the above selector?


I did, changed to:

var toAdd = $('input["checkListItem"]').val();

Gave me:

Error: Syntax error, unrecognized expression: ["checkListItem"]

I also tried it without the single quotes, gave a "input not defined" error.

Append to Body

Figured it out!

var toAdd = $('input[name=checkListItem]').val();

I didn't have the name= part of the above code. Once I added that, toAdd returned the input from the add form. Persistence :slight_smile:


And I only had to tell you twice... Well done.


This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.