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


#1



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>');
    });
});


#2

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

'input[name=checkListItem]'

We need to include the attribute name with its value.

name="checkListItem"

The above is how it appears in the HTML.


#3

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:


#4

Did you correct syntax in the above selector?


#5

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
#6

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:


#7

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


#8

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