lakenney
lakenney

Reputation: 89

HTML Div has no method appendTo()

I'm getting this error in the console: Uncaught TypeError: Object #<HTMLDivElement> has no method 'appendTo' JS File

$('#refresh-button').click(function() 

Upvotes: 0

Views: 276

Answers (3)

Elmor
Elmor

Reputation: 4895

Maybe I'm missing a point... but I don't see where these variables are set

    product_title
    product_shape
    product_size
    product_metal
    product_stock
    product_price

Upvotes: 0

lonesomeday
lonesomeday

Reputation: 237995

I'm not quite sure where those variables are coming from, but the problem is that they are pointing to native DOM elements, not to jQuery selections. appendTo is a jQuery function, not a DOM function.

The easy solution is to use the jQuery selection you're already creating:

product_title.appendTo($("#product_name"));
// becomes
$("#product_name").append(product_title);

and likewise for the other variables.

The other way is to turn the native element into a jQuery selection with the $() wrapper, but that would be less efficient, as you'd have to create two selections:

$(product_title).appendTo($("#product_name"));

Upvotes: 1

Benjamin Gruenbaum
Benjamin Gruenbaum

Reputation: 276496

The error make sense. HTML Elements indeed do not have an appendTo method.

You can convert your HTML Div element to a jQuery object by passing it to jQuery

var jqObject = $(myDivElement); // now I can call appendTo on jqObject
jqObject.appendTo(parent);

Alternatively, you can use the native DOM method:

// this also works, assuming theParent is a DOM element too
theParent.appendChild(myDivElement); 
// of if it's a jQuery object
theParent.append(myDivelement);

Upvotes: 1

Related Questions