I have three buttons which clone a div
and it will show the div
before the clicked button. Currently when clicking on a button, the div
will appear before all three buttons. I need on click of the button to insert that div
only before the clicked button.
var toAppend = $('#appendFullWidthAll').children();
$('#showContent').click(function() {
toAppend.clone().insertBefore('.show');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div id="appendFullWidthAll" class="d-none">
<ul>
<li>Test Content</li>
<li>Test Content</li>
<li>Test Content</li>
<li>Test Content</li>
</ul>
</div>
<div class="1">
<a class="btn btn-primary show" id="showContent">Click Here</a>
</div>
<div class="2">
<a class="btn btn-primary show" id="showContent">Click Here</a>
</div>
<div class="3">
<a class="btn btn-primary show" id="showContent">Click Here</a>
</div>
You are inserting before of any element that have the class show
, that is your main problem. The solution is to capture the element you have clicked using $(this)
selector, like this:
$('.btn').click(function() {
toAppend.clone().insertBefore($(this));
});
Firstly, IDs in HTML must be unique. Now, to apply any action to the element that raised the event, use the keyword this
inside the event. But remember that this
is a JavaScript keyword, so use $(this)
to convert it to jQuery:
var toAppend = $('#appendFullWidthAll').children();
$('.show').click(function() {
toAppend.clone().insertBefore(this);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div id="appendFullWidthAll" class="d-none">
<ul>
<li>Test Content</li>
<li>Test Content</li>
<li>Test Content</li>
<li>Test Content</li>
</ul>
</div>
<div class="1">
<a class="btn btn-primary show">Click Here</a>
</div>
<div class="2">
<a class="btn btn-primary show">Click Here</a>
</div>
<div class="3">
<a class="btn btn-primary show">Click Here</a>
</div>
You can get the element you have clicked using the $(this)
selector inside the click
event listener. Also, don't use the same id
attribute for multiple elements, I have replaced the click
listener using the show
class selector instead of the wrong one that uses the id
attribute. Check next example:
$(document).ready(function()
{
var toAppend = $('#appendFullWidthAll').children();
$('.show').click(function()
{
var clickedButton = $(this)
toAppend.clone().insertBefore(clickedButton);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="appendFullWidthAll" class="d-none">
<ul>
<li>Test Content</li>
<li>Test Content</li>
<li>Test Content</li>
<li>Test Content</li>
</ul>
</div>
<div class="1">
<a class="btn btn-primary show" id="showContent1">Click Here</a>
</div>
<div class="2">
<a class="btn btn-primary show" id="showContent2">Click Here</a>
</div>
<div class="3">
<a class="btn btn-primary show" id="showContent3">Click Here</a>
</div>
I have a simple 3 part horizontal bar chart like so: You can check it out on CodeSandbox or try out the code: function App() { return ( <VictoryStack colorScale={['#D0021B', '#F5A623', '#...
I have a simple 3 part horizontal bar chart like so: You can check it out on CodeSandbox or try out the code: function App() { return ( <VictoryStack colorScale={['#D0021B', '#F5A623', '#...
i am trying to set up an user level authentication using node.js, so i go and do npm install -g jsonwebtoken --save. However, i run into problems when i use require('jsonwebtoken'); and try to compile ...
i am trying to set up an user level authentication using node.js, so i go and do npm install -g jsonwebtoken --save. However, i run into problems when i use require('jsonwebtoken'); and try to compile ...
I just found an unexpected behavior where I have placed a timeStamp (Date) inside a hidden form field. When submitting the (update) form I made some checks on the document delta (because I only want ...
I just found an unexpected behavior where I have placed a timeStamp (Date) inside a hidden form field. When submitting the (update) form I made some checks on the document delta (because I only want ...
In this code example: let result = (async (global) => { // other code here (includes await's; thus the async) return 123; })(this); The code works, but the return'ed value is nowhere to be ...
In this code example: let result = (async (global) => { // other code here (includes await's; thus the async) return 123; })(this); The code works, but the return'ed value is nowhere to be ...