Jquery change div created from html before adding

I have a function that adds a div to another div.

Sort of:

var div = ...some html

$("#mydiv").append(div);

However, I want to add something to the div I just added. Currently, I want to hide the sub-item of the new div and add the object as data. Sort of

 var div = ...some html   
 $("#mydiv").append(div);
 $(my new div).find(".subDiv").hide(); 
 $(my new div).data('user',object);

But how do I get the new div that I created? Is there a way to create it and then preform these actions and then add it? Or should I add it and then extract it and change it?

Efficiency is important as it will be repeated for search results ...

Thank!

I used this as my solution thanks to Tricker:

var div = ...a lagre piece of html;
var newDiv = $(div);
newDiv.find("[show='contractor']").hide();
newDiv.data('user', userObject);
$(appendDiv).append(newDiv);

Thank!

+3
source share
3 answers

As you want, like this:

var new_obj = $('<div class="subDiv"></div>');

$("#my_div").append(new_obj);

//You dont have to find the subdiv because the object "new_obj" is your subDiv
new_obj.hide();
new_obj.data('user',object);
+4
source

.append() ?

var myNewDiv= $("#mydiv").append(div);
+2

div, , $("#myDiv div:last-child")

0

Source: https://habr.com/ru/post/1782605/


All Articles