I hav...">

Adding div to another dynamically using jQuery

Suppose I have the following div

<div id="myDiv" style="display:none" title=""></div>

I have an ajax call that adds HTML markup to this div using

$("#myDiv").html('').html(response);

I would like to add hidden content to the main div before the response content, so the result would be

<div id="myDiv" style="display:none" title="">
    //my hidden content
    //here there will be the response HTML markup    
</div>

How to do this using jQuery code?

+3
source share
2 answers

Since it is .html()overwritten, there is no need to do .html('').

The hidden content will be installed first, and will respond.append() .

$("#myDiv").html('<span class="hidden">somehiddencontent</span>')
           .append(response);

CSS

span.hidden { display:none; }

You can also do this with one shot:

$("#myDiv").html('<span class="hidden">somehiddencontent</span>' + response);

, #myDiv jQuery, .empty() .html().

$("#myDiv").empty()
           .html('<span class="hidden">somehiddencontent</span>' + response);
+5
$("#myDiv").html('').append(something).append(somethingElse);

prepend(), .

+1

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


All Articles