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.
jquery html
|
show 1 more comment
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.
jquery html
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 theinnerHTML
of the#appendFullWidth
element, which has noappend()
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
|
show 1 more comment
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.
jquery html
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
jquery html
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 theinnerHTML
of the#appendFullWidth
element, which has noappend()
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
|
show 1 more comment
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 theinnerHTML
of the#appendFullWidth
element, which has noappend()
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
|
show 1 more comment
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
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 theinnerHTML
of the#appendFullWidth
element, which has noappend()
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