How to append HTML block and insert before a specific element











up vote
-2
down vote

favorite












I have a button which on click It will append a hidden HTML block, before a certain element. I know this is a very simple question but I was struggling to find out the correct way of doing it. Following is my snippet.



$("#layout-full-width").click(function(){
$('#appendFullWidth').html().append().before('#layout-container');
});


HTML



<button class="btn btn-primary" id="layout-full-width">Append</button>

<div id="#layout-container">content</div>

<div class="hidden" id="appendFullWidth">
<div class="col-md-12 layout-fullwidth text-center m-y-10">
<div class="layout-inner-container">
<a id="layoutRemove" class="btn btn-circle-table red btn-alignment" data-toggle="tooltip" data-placement="top" title="" data-original-title="Delete"><i class="fa fa-trash m-a-0"></i></a>
<p class="m-a-0 layout-inner-text"><i class="fa fa-plus-circle m-r-10" aria-hidden="true"></i> Hover to add a component</p>
<ul class="list-inline layout-components">
<li data-toggle="tooltip" data-placement="top" title="Textarea"><a class="layoutTextArea"><i class="fa fa-file-text-o" aria-hidden="true"></i></a></li>
<li data-toggle="tooltip" data-placement="top" title="Title"><a href="#"><i class="fa fa-font" aria-hidden="true"></i></a></li>
<li data-toggle="tooltip" data-placement="top" title="Question"><a href="#"><i class="fa fa-question" aria-hidden="true"></i></a></li>
<li data-toggle="tooltip" data-placement="top" title="Attachment"><a href="#"><i class="fa fa-paperclip" aria-hidden="true"></i></a></li>
<li data-toggle="tooltip" data-placement="top" title="Embed"><a href="#"><i class="fa fa-picture-o" aria-hidden="true"></i></a></li>
<li data-toggle="tooltip" data-placement="top" title="URL"><a href="#"><i class="fa fa-link" aria-hidden="true"></i></a></li>
<li data-toggle="tooltip" data-placement="top" title="" data-original-title="Tags"><a href="#"><i class="fa fa-tags" aria-hidden="true"></i></a></li>
</ul>
</div>
</div>




How to append #appendFullWidth html content before #layout-container element. The above code does not work.










share|improve this question
























  • please see the last snippet. #insertHere for an example
    – Ganidu Ranasinghe
    Nov 11 at 10:00










  • share your html code
    – DPS
    Nov 11 at 10:02










  • See following code. $('#appendFullWidth').html().append().insertBefore('#layout-container'); I need to append the html content of #appendFullWidth before #layout-container element
    – Ganidu Ranasinghe
    Nov 11 at 10:17






  • 1




    how we supose to know your html structure?
    – Smollet777
    Nov 11 at 10:27






  • 1




    $('#appendFullWidth').html() returns the innerHTML of the #appendFullWidth element, which has no append() method. Did you look at any of the - remarkably good - documentation? Please: read the documentation of the methods you're trying to use: html(), append(), insertBefore().
    – David Thomas
    Nov 11 at 10:35

















up vote
-2
down vote

favorite












I have a button which on click It will append a hidden HTML block, before a certain element. I know this is a very simple question but I was struggling to find out the correct way of doing it. Following is my snippet.



$("#layout-full-width").click(function(){
$('#appendFullWidth').html().append().before('#layout-container');
});


HTML



<button class="btn btn-primary" id="layout-full-width">Append</button>

<div id="#layout-container">content</div>

<div class="hidden" id="appendFullWidth">
<div class="col-md-12 layout-fullwidth text-center m-y-10">
<div class="layout-inner-container">
<a id="layoutRemove" class="btn btn-circle-table red btn-alignment" data-toggle="tooltip" data-placement="top" title="" data-original-title="Delete"><i class="fa fa-trash m-a-0"></i></a>
<p class="m-a-0 layout-inner-text"><i class="fa fa-plus-circle m-r-10" aria-hidden="true"></i> Hover to add a component</p>
<ul class="list-inline layout-components">
<li data-toggle="tooltip" data-placement="top" title="Textarea"><a class="layoutTextArea"><i class="fa fa-file-text-o" aria-hidden="true"></i></a></li>
<li data-toggle="tooltip" data-placement="top" title="Title"><a href="#"><i class="fa fa-font" aria-hidden="true"></i></a></li>
<li data-toggle="tooltip" data-placement="top" title="Question"><a href="#"><i class="fa fa-question" aria-hidden="true"></i></a></li>
<li data-toggle="tooltip" data-placement="top" title="Attachment"><a href="#"><i class="fa fa-paperclip" aria-hidden="true"></i></a></li>
<li data-toggle="tooltip" data-placement="top" title="Embed"><a href="#"><i class="fa fa-picture-o" aria-hidden="true"></i></a></li>
<li data-toggle="tooltip" data-placement="top" title="URL"><a href="#"><i class="fa fa-link" aria-hidden="true"></i></a></li>
<li data-toggle="tooltip" data-placement="top" title="" data-original-title="Tags"><a href="#"><i class="fa fa-tags" aria-hidden="true"></i></a></li>
</ul>
</div>
</div>




How to append #appendFullWidth html content before #layout-container element. The above code does not work.










share|improve this question
























  • please see the last snippet. #insertHere for an example
    – Ganidu Ranasinghe
    Nov 11 at 10:00










  • share your html code
    – DPS
    Nov 11 at 10:02










  • See following code. $('#appendFullWidth').html().append().insertBefore('#layout-container'); I need to append the html content of #appendFullWidth before #layout-container element
    – Ganidu Ranasinghe
    Nov 11 at 10:17






  • 1




    how we supose to know your html structure?
    – Smollet777
    Nov 11 at 10:27






  • 1




    $('#appendFullWidth').html() returns the innerHTML of the #appendFullWidth element, which has no append() method. Did you look at any of the - remarkably good - documentation? Please: read the documentation of the methods you're trying to use: html(), append(), insertBefore().
    – David Thomas
    Nov 11 at 10:35















up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I have a button which on click It will append a hidden HTML block, before a certain element. I know this is a very simple question but I was struggling to find out the correct way of doing it. Following is my snippet.



$("#layout-full-width").click(function(){
$('#appendFullWidth').html().append().before('#layout-container');
});


HTML



<button class="btn btn-primary" id="layout-full-width">Append</button>

<div id="#layout-container">content</div>

<div class="hidden" id="appendFullWidth">
<div class="col-md-12 layout-fullwidth text-center m-y-10">
<div class="layout-inner-container">
<a id="layoutRemove" class="btn btn-circle-table red btn-alignment" data-toggle="tooltip" data-placement="top" title="" data-original-title="Delete"><i class="fa fa-trash m-a-0"></i></a>
<p class="m-a-0 layout-inner-text"><i class="fa fa-plus-circle m-r-10" aria-hidden="true"></i> Hover to add a component</p>
<ul class="list-inline layout-components">
<li data-toggle="tooltip" data-placement="top" title="Textarea"><a class="layoutTextArea"><i class="fa fa-file-text-o" aria-hidden="true"></i></a></li>
<li data-toggle="tooltip" data-placement="top" title="Title"><a href="#"><i class="fa fa-font" aria-hidden="true"></i></a></li>
<li data-toggle="tooltip" data-placement="top" title="Question"><a href="#"><i class="fa fa-question" aria-hidden="true"></i></a></li>
<li data-toggle="tooltip" data-placement="top" title="Attachment"><a href="#"><i class="fa fa-paperclip" aria-hidden="true"></i></a></li>
<li data-toggle="tooltip" data-placement="top" title="Embed"><a href="#"><i class="fa fa-picture-o" aria-hidden="true"></i></a></li>
<li data-toggle="tooltip" data-placement="top" title="URL"><a href="#"><i class="fa fa-link" aria-hidden="true"></i></a></li>
<li data-toggle="tooltip" data-placement="top" title="" data-original-title="Tags"><a href="#"><i class="fa fa-tags" aria-hidden="true"></i></a></li>
</ul>
</div>
</div>




How to append #appendFullWidth html content before #layout-container element. The above code does not work.










share|improve this question















I have a button which on click It will append a hidden HTML block, before a certain element. I know this is a very simple question but I was struggling to find out the correct way of doing it. Following is my snippet.



$("#layout-full-width").click(function(){
$('#appendFullWidth').html().append().before('#layout-container');
});


HTML



<button class="btn btn-primary" id="layout-full-width">Append</button>

<div id="#layout-container">content</div>

<div class="hidden" id="appendFullWidth">
<div class="col-md-12 layout-fullwidth text-center m-y-10">
<div class="layout-inner-container">
<a id="layoutRemove" class="btn btn-circle-table red btn-alignment" data-toggle="tooltip" data-placement="top" title="" data-original-title="Delete"><i class="fa fa-trash m-a-0"></i></a>
<p class="m-a-0 layout-inner-text"><i class="fa fa-plus-circle m-r-10" aria-hidden="true"></i> Hover to add a component</p>
<ul class="list-inline layout-components">
<li data-toggle="tooltip" data-placement="top" title="Textarea"><a class="layoutTextArea"><i class="fa fa-file-text-o" aria-hidden="true"></i></a></li>
<li data-toggle="tooltip" data-placement="top" title="Title"><a href="#"><i class="fa fa-font" aria-hidden="true"></i></a></li>
<li data-toggle="tooltip" data-placement="top" title="Question"><a href="#"><i class="fa fa-question" aria-hidden="true"></i></a></li>
<li data-toggle="tooltip" data-placement="top" title="Attachment"><a href="#"><i class="fa fa-paperclip" aria-hidden="true"></i></a></li>
<li data-toggle="tooltip" data-placement="top" title="Embed"><a href="#"><i class="fa fa-picture-o" aria-hidden="true"></i></a></li>
<li data-toggle="tooltip" data-placement="top" title="URL"><a href="#"><i class="fa fa-link" aria-hidden="true"></i></a></li>
<li data-toggle="tooltip" data-placement="top" title="" data-original-title="Tags"><a href="#"><i class="fa fa-tags" aria-hidden="true"></i></a></li>
</ul>
</div>
</div>




How to append #appendFullWidth html content before #layout-container element. The above code does not work.







jquery html






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 10:36

























asked Nov 11 at 9:55









Ganidu Ranasinghe

217




217












  • please see the last snippet. #insertHere for an example
    – Ganidu Ranasinghe
    Nov 11 at 10:00










  • share your html code
    – DPS
    Nov 11 at 10:02










  • See following code. $('#appendFullWidth').html().append().insertBefore('#layout-container'); I need to append the html content of #appendFullWidth before #layout-container element
    – Ganidu Ranasinghe
    Nov 11 at 10:17






  • 1




    how we supose to know your html structure?
    – Smollet777
    Nov 11 at 10:27






  • 1




    $('#appendFullWidth').html() returns the innerHTML of the #appendFullWidth element, which has no append() method. Did you look at any of the - remarkably good - documentation? Please: read the documentation of the methods you're trying to use: html(), append(), insertBefore().
    – David Thomas
    Nov 11 at 10:35




















  • please see the last snippet. #insertHere for an example
    – Ganidu Ranasinghe
    Nov 11 at 10:00










  • share your html code
    – DPS
    Nov 11 at 10:02










  • See following code. $('#appendFullWidth').html().append().insertBefore('#layout-container'); I need to append the html content of #appendFullWidth before #layout-container element
    – Ganidu Ranasinghe
    Nov 11 at 10:17






  • 1




    how we supose to know your html structure?
    – Smollet777
    Nov 11 at 10:27






  • 1




    $('#appendFullWidth').html() returns the innerHTML of the #appendFullWidth element, which has no append() method. Did you look at any of the - remarkably good - documentation? Please: read the documentation of the methods you're trying to use: html(), append(), insertBefore().
    – David Thomas
    Nov 11 at 10:35


















please see the last snippet. #insertHere for an example
– Ganidu Ranasinghe
Nov 11 at 10:00




please see the last snippet. #insertHere for an example
– Ganidu Ranasinghe
Nov 11 at 10:00












share your html code
– DPS
Nov 11 at 10:02




share your html code
– DPS
Nov 11 at 10:02












See following code. $('#appendFullWidth').html().append().insertBefore('#layout-container'); I need to append the html content of #appendFullWidth before #layout-container element
– Ganidu Ranasinghe
Nov 11 at 10:17




See following code. $('#appendFullWidth').html().append().insertBefore('#layout-container'); I need to append the html content of #appendFullWidth before #layout-container element
– Ganidu Ranasinghe
Nov 11 at 10:17




1




1




how we supose to know your html structure?
– Smollet777
Nov 11 at 10:27




how we supose to know your html structure?
– Smollet777
Nov 11 at 10:27




1




1




$('#appendFullWidth').html() returns the innerHTML of the #appendFullWidth element, which has no append() method. Did you look at any of the - remarkably good - documentation? Please: read the documentation of the methods you're trying to use: html(), append(), insertBefore().
– David Thomas
Nov 11 at 10:35






$('#appendFullWidth').html() returns the innerHTML of the #appendFullWidth element, which has no append() method. Did you look at any of the - remarkably good - documentation? Please: read the documentation of the methods you're trying to use: html(), append(), insertBefore().
– David Thomas
Nov 11 at 10:35














1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










First of all $.html() method in jQuery give you html string and not the jq element.



and now the solution:
There is 1 mistake in you html code id of you div is #layout-container it should be layout-container without the #.
After changing this following code should work as you expect.



$("#layout-full-width").click(function(){
var toAppend=$('#appendFullWidth').children().eq(0);
toAppend.prependTo('#layout-container');

});


Here is working js bin link.https://jsbin.com/ninonul/1/edit?html,js,output






share|improve this answer





















  • Thanks for your answer. This is working fine. However I want to keep adding the html blocks over and over when clicking on the #layout-full-width.
    – Ganidu Ranasinghe
    Nov 11 at 11:01










  • prepend() method removes element from first place, so there is nothing to append second time. However this can be solve by .clone() method. toAppend.clone().prependTo('#layout-container'); Also updated the jsbin on this link jsbin.com/mazokuq/edit?html,js,output.
    – Ninad
    Nov 11 at 11:13












  • This is what I was looking for. Thank you
    – Ganidu Ranasinghe
    Nov 11 at 11:40











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247564%2fhow-to-append-html-block-and-insert-before-a-specific-element%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










First of all $.html() method in jQuery give you html string and not the jq element.



and now the solution:
There is 1 mistake in you html code id of you div is #layout-container it should be layout-container without the #.
After changing this following code should work as you expect.



$("#layout-full-width").click(function(){
var toAppend=$('#appendFullWidth').children().eq(0);
toAppend.prependTo('#layout-container');

});


Here is working js bin link.https://jsbin.com/ninonul/1/edit?html,js,output






share|improve this answer





















  • Thanks for your answer. This is working fine. However I want to keep adding the html blocks over and over when clicking on the #layout-full-width.
    – Ganidu Ranasinghe
    Nov 11 at 11:01










  • prepend() method removes element from first place, so there is nothing to append second time. However this can be solve by .clone() method. toAppend.clone().prependTo('#layout-container'); Also updated the jsbin on this link jsbin.com/mazokuq/edit?html,js,output.
    – Ninad
    Nov 11 at 11:13












  • This is what I was looking for. Thank you
    – Ganidu Ranasinghe
    Nov 11 at 11:40















up vote
0
down vote



accepted










First of all $.html() method in jQuery give you html string and not the jq element.



and now the solution:
There is 1 mistake in you html code id of you div is #layout-container it should be layout-container without the #.
After changing this following code should work as you expect.



$("#layout-full-width").click(function(){
var toAppend=$('#appendFullWidth').children().eq(0);
toAppend.prependTo('#layout-container');

});


Here is working js bin link.https://jsbin.com/ninonul/1/edit?html,js,output






share|improve this answer





















  • Thanks for your answer. This is working fine. However I want to keep adding the html blocks over and over when clicking on the #layout-full-width.
    – Ganidu Ranasinghe
    Nov 11 at 11:01










  • prepend() method removes element from first place, so there is nothing to append second time. However this can be solve by .clone() method. toAppend.clone().prependTo('#layout-container'); Also updated the jsbin on this link jsbin.com/mazokuq/edit?html,js,output.
    – Ninad
    Nov 11 at 11:13












  • This is what I was looking for. Thank you
    – Ganidu Ranasinghe
    Nov 11 at 11:40













up vote
0
down vote



accepted







up vote
0
down vote



accepted






First of all $.html() method in jQuery give you html string and not the jq element.



and now the solution:
There is 1 mistake in you html code id of you div is #layout-container it should be layout-container without the #.
After changing this following code should work as you expect.



$("#layout-full-width").click(function(){
var toAppend=$('#appendFullWidth').children().eq(0);
toAppend.prependTo('#layout-container');

});


Here is working js bin link.https://jsbin.com/ninonul/1/edit?html,js,output






share|improve this answer












First of all $.html() method in jQuery give you html string and not the jq element.



and now the solution:
There is 1 mistake in you html code id of you div is #layout-container it should be layout-container without the #.
After changing this following code should work as you expect.



$("#layout-full-width").click(function(){
var toAppend=$('#appendFullWidth').children().eq(0);
toAppend.prependTo('#layout-container');

});


Here is working js bin link.https://jsbin.com/ninonul/1/edit?html,js,output







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 10:56









Ninad

916




916












  • Thanks for your answer. This is working fine. However I want to keep adding the html blocks over and over when clicking on the #layout-full-width.
    – Ganidu Ranasinghe
    Nov 11 at 11:01










  • prepend() method removes element from first place, so there is nothing to append second time. However this can be solve by .clone() method. toAppend.clone().prependTo('#layout-container'); Also updated the jsbin on this link jsbin.com/mazokuq/edit?html,js,output.
    – Ninad
    Nov 11 at 11:13












  • This is what I was looking for. Thank you
    – Ganidu Ranasinghe
    Nov 11 at 11:40


















  • Thanks for your answer. This is working fine. However I want to keep adding the html blocks over and over when clicking on the #layout-full-width.
    – Ganidu Ranasinghe
    Nov 11 at 11:01










  • prepend() method removes element from first place, so there is nothing to append second time. However this can be solve by .clone() method. toAppend.clone().prependTo('#layout-container'); Also updated the jsbin on this link jsbin.com/mazokuq/edit?html,js,output.
    – Ninad
    Nov 11 at 11:13












  • This is what I was looking for. Thank you
    – Ganidu Ranasinghe
    Nov 11 at 11:40
















Thanks for your answer. This is working fine. However I want to keep adding the html blocks over and over when clicking on the #layout-full-width.
– Ganidu Ranasinghe
Nov 11 at 11:01




Thanks for your answer. This is working fine. However I want to keep adding the html blocks over and over when clicking on the #layout-full-width.
– Ganidu Ranasinghe
Nov 11 at 11:01












prepend() method removes element from first place, so there is nothing to append second time. However this can be solve by .clone() method. toAppend.clone().prependTo('#layout-container'); Also updated the jsbin on this link jsbin.com/mazokuq/edit?html,js,output.
– Ninad
Nov 11 at 11:13






prepend() method removes element from first place, so there is nothing to append second time. However this can be solve by .clone() method. toAppend.clone().prependTo('#layout-container'); Also updated the jsbin on this link jsbin.com/mazokuq/edit?html,js,output.
– Ninad
Nov 11 at 11:13














This is what I was looking for. Thank you
– Ganidu Ranasinghe
Nov 11 at 11:40




This is what I was looking for. Thank you
– Ganidu Ranasinghe
Nov 11 at 11:40


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247564%2fhow-to-append-html-block-and-insert-before-a-specific-element%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine