3

Suppose I have the following DIV

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

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

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

I would like to append 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 can I do it using jQuery code?

4
  • Just add another div after your hidden content and add to that? Commented Nov 13, 2010 at 16:21
  • do you want to have a permanent hidden content and replace each time his following siblings ? Commented Nov 13, 2010 at 16:23
  • With the code I am using I have one child node of myDiv being added. I want to add another child node of myDiv before the one that Commented Nov 13, 2010 at 16:24
  • @Riccardo Galli: Yes that one is an alternative that I can follow Commented Nov 13, 2010 at 16:26

2 Answers 2

5

Since .html() overwrites anyway, there's really no need to do .html('').

This will first set the hidden content, then it will .append() the response.

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

CSS

span.hidden { display:none; }

You could also do it in one shot:

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

If there's any chance that there will be jQuery managed data inside #myDiv, it would be safer to use .empty() before .html().

$("#myDiv").empty()
           .html('<span class="hidden">somehiddencontent</span>' + response);
Sign up to request clarification or add additional context in comments.

1 Comment

Good. I am going to try it. I am trying to do this just to solve the problem described here: http://stackoverflow.com/questions/4172511/jquery-generic-code can you have a look at it?
1
$("#myDiv").html('').append(something).append(somethingElse);

or prepend() if you want to do it in the opposite order.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.