12/14 Append to Body


#1

Would someone be nice to explain what's the purpose of + toAdd +?

why do we need the plus sign? when not just this 'toAdd'?


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


#2

Because if you don't include the + signs, the computer wouldn't know what you want it to do. The + signs tell the computer to add two things together. In this case, you want to add
<div class="item">' and ("input[name=checkListItem]").val() and a closing tag, '</div>'.
'

When you put + signs the computer thinks "I have to put this string together with this variable and finally together with this string". Otherwise the computer thinks "ERROR ERROR ERROR".


#3

I'm curious why we need to add so many hypotheses. Why can't it be:

$(".list").append('

+ toAdd + </div');

Why does the beginning and closing div tag need its own set of hypotheses?


#4

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