Why the result is undefined?


#1


https://www.codecademy.com/courses/web-beginner-en-v6phg/2/5?curriculum_id=50a3fad8c7a770b5fd0007a1


Why when I run my code like this the result of the input will be undefined but if I replaced the

var toAdd = input;

with :

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

it will work correctly !


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


#2

@ahmedlebda
Maybe you should read why the otherway is correct
https://discuss.codecademy.com/t/val-but-why/11772/2?u=leonhard.wettengmx.n

or read
== the Book ==
https://learn.jquery.com/using-jquery-core/selecting-elements/#selecting-form-elements


#3

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