Ad

What Is The Type Of A String Added To A Property Of A Variable?

- 1 answer

ISSUE

I wanted to fix this error Uncaught TypeError: ('<li>' + $input.val() + '</li>').appendTo(ourList) is not a function.

I've been playing around a lot and I don't even remember how I ended up adding the $ in front of the string (?) inside the parentheses. It worked but I don't know why...


MY REASEARCH

From my understanding based off of MDN I got that error in my code because the function was called on the wrong object.

Then the API Docs says that this function can be called on any of those Selector | htmlString | Element | Array | jQuery object


So I fixed the error when I called the .appenTo method on a jQ object

$('<li>' + $input.val() + '</li>').appendTo(ourList); // jQuery object

But I got this error from calling the .appenTo method on an htmlString, though that should be fine... I mean, this bit of code ('<li>' + $input.val() + '</li>') Is a string, isn't it?

('<li>' + $input.val() + '</li>').appendTo(ourList); // htmlString???    

MY QUESTION

What is up with this bit of code ('<li>' + $input.val() + '</li>') that it caused the error? Is it a string or not?

Ad

Answer

Then the API Docs says that this function can be called on any of those Selector | htmlString | Element | Array | jQuery object

Nope it says that the target parameter takes these types, so you can do:

jQObject
   .appendTo("#test") // selector
   .appendTo("<div></div>") // html string
   .appendTo(document.body) // element
   .appendTo([document.body]) // array
   .appendTo($()) // jQObject

But it can only be called on a jquery object, not on a plain string.

Ad
source: stackoverflow.com
Ad