Threadly


#1

Hi everyone, I'm trying to solve Jquery Projects - Threadly. When i input the text, it does not show up in the list. any solutions? Thanks.

var main = function() {
  $('form').submit(function() {
    var comment = $('.comment').val();
    
    if(comment !== "") {
      var html = $('<li>').text(comment);
      html.prependTo('.comments');
      $('.comment').val() = "";
      
    }

    return false;
  });
};

$(document).ready(main);

#2

Hey Albert,

$('.comment') selects an element with the class="comment" attribute. You need to select the comment id. Do you know how to do that?

Hint

With jQuery, you can select an id like this:

$('#id')
//  ↑ this gets replaced by the actual id (in this case, it's `comment`)


#3

var comment = $('.comment').val();
should be var comment = $('#comment').val();

html.prependTo('.comments');
can be html.prependTo('ul.comments');
It doesn't have to be but it is more specific.

$('.comment').val() = "";
should be $('.comment').val("");


#4

2 posts were split to a new topic: Need help with Threadly


#5

var $html = $('#comment').val(); should be var html = $('

  • ').text(comment); since your populating a li tag with the var comment which contains the comment text.

    html.prependTo('ul.comments'); is a more specific version of your $html.prependTo('.comments'); line which just makes sure the code is going to the right place.

    lastly $('#comment').val(""); should be inserted into your if statement because it causes the comment input to clear after the comment has been posted.

    Here is the code all together

    > if(comment !== "") {
    >       var html = $('<li>').text(comment);
    >       html.prependTo('ul.comments');
    >       $('#comment').val("");
    >     }