Javascript Variable Showing instead of Variable's Content

I have the following code, where I’m attempting to get a series of element_1, element_2 and so on, and then choose one of them at random.

With this code, where I have console.log(element_count); that will log 1, 2 and so on as expected.

However, all of this results in random_element literally becoming "element_2" instead of $('<div class="fruit oranges">2</div>').

Is it something with element_counted = 'element_' + element_count;?

jQuery(document).ready(function($) {
  var element_count = 0;

  var element_1 = $('<div class="fruit apples">1</div>');

  var element_count = element_count + 1,
    element_counted = 'element_' + element_count;
  console.log(element_count);

  var element_2 = $('<div class="fruit oranges">2</div>');

  var element_count = element_count + 1,
    element_counted = 'element_' + element_count;
  console.log(element_count);

  var random_element = element_counted; // This becomes 'element_2' instead of element_2's variable content.
  console.log(random_element);
  $('.element-placeholder').replaceWith(random_element);

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class=".element-placeholder">element_2</div>
<!--This should be one of the .fruit divs instead of the text "pane_2"-->

103 thoughts on “Javascript Variable Showing instead of Variable's Content”

  1. therapie cognitivo comportementale quimper pharmacie palix annecy , pharmacie de garde zoubir aujourd’hui traitement gastro , Comprar Motilium sin receta, Comprar Motilium medicamento. Motilium en farmacia Miami Comprar Motilium genГ©rico [url=https://kit.co/veostatat/donde-comprar-motilium-10-mg-comprar-motilium-sin-receta#]Motilium Domperidone 10 mg[/url] pharmacie angers ralliement Comprar Ciprofloxacine medicamento, Comprar Ciprofloxacine medicamento. Ciprofloxacine en farmacia EspaГ±a Comprar Ciprofloxacine genГ©rico [url=https://kit.co/spinobgui/comprar-ciprofloxacine-medicamento-comprar-ciprofloxacine-generico#]Comprar Ciprofloxacine sin receta[/url] , therapie cognitivo comportementale finistere pharmacie du stade annecy , pharmacie bailly cabannes. pharmacie ouverte quillan salon des therapies alternatives lorient Metformine en farmacia Argentina, DГіnde comprar Metformine 850 mg. Comprar Metformine sin receta Comprar Metformine genГ©rico [url=https://kit.co/lifode/comprar-metformine-medicamento-metformine-en-farmacia-argentina#]Comprar Metformine medicamento, Metformine en farmacia Argentina[/url] , medicaments morphine. pharmacie bordeaux achard, technique therapies breves pharmacie bourges ouverte lundi . pharmacie favier amiens telephone, pharmacie amiens jules barni pharmacie leclerc clichy sous bois pharmacie trousselle angers .

    Reply

Leave a Comment