Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

appendTo() is not a function?

Tags:

jquery

I thought this code should work but it didnt, can someone explain?

 $("#addLinkLayout input.comment, #addLinkLayout input.link").each(function() {
      $(this).val().appendTo('div#links');
 });

It says $(this).val().appendTo() is not a function.

like image 948
ajsie Avatar asked Jan 24 '10 20:01

ajsie


2 Answers

appendTo can only be applied on jQuery objects. But val returns a string.

Try this instead:

$("#addLinkLayout input.comment, #addLinkLayout input.link").each(function() {
    $('div#links').append($(this).val());
});
like image 146
Gumbo Avatar answered Sep 19 '22 15:09

Gumbo


val() doesn't return a DOM element. It returns the value attribute from a DOM element. So if you have something like <input value="foo" />, calling val() on that node will give you a string "foo". Since javascript's string class doesn't have a method appendTo, you're getting an error.

You probably want something like

$('div#links').append($(this).val());
like image 39
ShZ Avatar answered Sep 19 '22 15:09

ShZ