javascript - Jquery modify div created from html before appending - Stack Overflow

I have a function that appends a div to another div.Something like:var div = ...some html$("#mydiv

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

Something like:

var div = ...some html

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

However I want to do some stuff to the div I just added. Currently I want to hide a sub part of the new div, and add a object as data. Something like

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

But how should I get the the new div I created? Is there a way to create it and then preform these actions, and then append it? Or should I append it and then retrieve it and modify it?

Efficiency is important as this will be iterated for search results...

Thanks!

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);

Thanks!

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

Something like:

var div = ...some html

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

However I want to do some stuff to the div I just added. Currently I want to hide a sub part of the new div, and add a object as data. Something like

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

But how should I get the the new div I created? Is there a way to create it and then preform these actions, and then append it? Or should I append it and then retrieve it and modify it?

Efficiency is important as this will be iterated for search results...

Thanks!

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);

Thanks!

Share Improve this question edited Dec 29, 2010 at 15:53 kralco626 asked Dec 29, 2010 at 14:20 kralco626kralco626 8,66441 gold badges115 silver badges171 bronze badges 2
  • you can chain function calls in jQuery, and you really should read the jQuery API as the appendTo function would have suited your needs better. The entire thing could have been written as: $(div).data('user', userObject).appendTo(appendDiv).find('[show="contractor"]').hide(); – zzzzBov Commented Dec 29, 2010 at 16:16
  • does appendTo return the parent or child object? I looked in the API and I can't find it. However, this could be written as $(appendDiv).append($(div).data('user',userObject).find("[show-'contractor']").hide()); I think, and that would be the same thing? – kralco626 Commented Dec 29, 2010 at 18:04
Add a ment  | 

3 Answers 3

Reset to default 4

The way u want is 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);

Does .append() not return the new item?

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

since it will always be the last child div, I believe you can access it via $("#myDiv div:last-child")

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745119550a4612331.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信