Daniel
Daniel

Reputation: 1466

How To output form input value using appendTo with jquery

Alright Before Getting Started Here is the idea . i'm making a reminder app it's just a simple app . i'm getting the value from the input field and making a checkbox with the input value next to it . now i'm using appendTo method but it's not printing the value . if you guys can help it would be awesome thanks!

enter image description here

MY HTML

 <form id="form">
   <input class="form-control topSpaceFromRoof" type="text"id="getReminder"/>
   <p class="text-center">
      <input type="submit" value="submit" class="btn btn-default">
   </p>
 </form>

 <!-- here is the div in which i want to append check boxes -->
 <div class="checkbox">

 </div>

MY JAVASCRIPT

 (function(){

    var input = $('#getReminder');
    $this     = $(this);

    $( "form" ).submit(function() {
     if(input.val() == ""){
       input.addClass('warning').attr('placeholder','Please set the reminder').addClass('warning-red');
       return false;
     }
    else
    {
        input.removeClass('warning');
        $('<label>
            <input type="checkbox" id="checkboxSuccess" value="option1">
            '+ input.val() +'
           </label>').appendTo('.checkbox');
        return true;
    }

});

})();

Upvotes: 0

Views: 242

Answers (2)

George
George

Reputation: 36784

In JavaScript, a new line within a string denotes the start of a new statement. If you want your string to cover multiple lines, you need to escape the carriage return:

 $('<label>\
    <input type="checkbox" id="checkboxSuccess" value="option1">\
    '+ input.val() +'\
    </label>').appendTo('.checkbox');

Or, concatenate each line:

 $('<label>' +
    '<input type="checkbox" id="checkboxSuccess" value="option1">' +
    input.val() +
    '</label>').appendTo('.checkbox');

Upvotes: 2

bcr
bcr

Reputation: 3811

else { 
   input.removeClass('warning');
    $('.checkbox').append('<label><input type="checkbox" id="checkboxSuccess" value="option1"> ' + input.val() + '</label>');
    return true;
 }

I'm not sure .appendTo() is what you want to use here, I would select the element with $(selector) and then use the .append() method to add the next item.

Upvotes: 0

Related Questions