JavaScript Find closest and insert before an element

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>
Answer:1

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));
});
Answer:2

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>
Answer:3

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>
Answer:4

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 ...

  1. cannot find module 'node-sass'
  2. cannot find module '@angular/core'
  3. cannot find module '@angular/http'
  4. cannot find module '@angular/compiler-cli'
  5. cannot find module 'typescript'
  6. cannot find module 'express'
  7. cannot find module '@babel/core'
  8. cannot find module '@angular/compiler-cli/ngcc'
  9. cannot find module providing package
  10. cannot find module '@angular-devkit/build-angular/package.json'
  11. cannot find module '../lib/utils/unsupported.js'
  12. cannot find module 'webpack-cli/bin/config-yargs'
  13. cannot find module for path
  14. cannot find module 'rxjs-compat/observable'
  15. cannot find module 'webpack'
  16. cannot find module react
  17. cannot find module angular
  18. cannot find module 'babel-preset-env'
  19. cannot find module '@babel/runtime/helpers/interoprequiredefault'
  20. cannot find module 'babel-preset-es2015'

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 ...

  1. javascript date from string
  2. javascript date from timestamp
  3. javascript date from milliseconds
  4. javascript date from epoch
  5. javascript date from utc
  6. javascript date from seconds
  7. javascript date from number
  8. javascript date from ticks
  9. javascript date from datetime
  10. javascript date from string format
  11. javascript date from time
  12. javascript date from year month day
  13. javascript date from json
  14. javascript date from ms
  15. javascript date from now
  16. javascript date from utc timestamp
  17. javascript date from string with timezone
  18. javascript date from string without timezone
  19. javascript date from yyyy-mm-dd
  20. javascript date from mm/dd/yyyy

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 ...

  1. possible value of x
  2. possible value of random variable
  3. possible value of position attribute
  4. possible value of the correlation coefficient
  5. possible value of probability
  6. possible value of n
  7. possible value of slope
  8. possible value of quadratic equation
  9. possible value of triangle
  10. possible value of