Yehuda Schwartz
Yehuda Schwartz

Reputation: 3503

js append div to only current parents child

I have a div block that is repeated on the page for each product. I am using JS to cycle through all the instances of a div class and would like to move that div to another div in the same block. I wrote this:

$(value).appendTo('.product-image')

and it adds each div to all the product-image divs on the page. How can I add each div only to the product-image div that is a child of $(values) parent?

This did not work for me:

$(value).appendTo($(value).parent().find('.product-image'))

Upvotes: 1

Views: 97

Answers (3)

Yehuda Schwartz
Yehuda Schwartz

Reputation: 3503

i came up with this sulotion thanks !

ind++
var tryy = "try"+ind;
$(value).parent().find('.product-image').addClass(tryy);
var tryyy = '.'+tryy;
$(value).appendTo(tryyy);

Upvotes: 1

AlexBerd
AlexBerd

Reputation: 1504

I think this will give you some ideas for implementation:

For example if you value element has value class:

$('.value:has(.product-image)').find('.product-image').each(function(){
     $(this).append(your_new_div)
});

Upvotes: 1

ahervin
ahervin

Reputation: 461

Try this to select the first parent div. $(value).appendTo($(value).parent('div:first()').find('.product-image'))

Upvotes: 1

Related Questions