Changing position of several DIVS with same selector in Jquery

1

I have a page that contains several structures with the same selector, such as:

<div class="preco">
R$ 69,90
</div>

<div class="preco-a-vista">
R$ 50,00
</div>

As I mentioned, the above structure repeats several times on the page. I need to put the content of div.preco-a-vista within div.preco

I made the following code:

$('.preco-avista').each(function() {  
  var $this = $(this);
  $('.preco').append($($this).html());
});

This code works, but it takes all the prices of the page (from the pre-a-view div) and puts all the price div. Repeating everything. Could you help me with this question?

    
asked by anonymous 25.06.2018 / 03:17

3 answers

1

I did not quite understand your question, but ...

$('.preco-a-vista').each((i,e) => { $('.preco').eq(i).html(e.textContent); })

    
25.06.2018 / 11:43
1

I made the function very simple and put the explanation commenting the lines of codes. But in summary:

2 Each of these elements is placed inside the element with class "price" in the same order;

3. "Preview" elements are deleted

$(function(){
  //pega a quantidade de elementos
  var vista = $(".preco-a-vista");
  var qntVista = vista.length;

  //função para remover os elementos após a execução
  function removerVista(){
    $(".preco-a-vista").remove(); 
  }

  // colocar os 'preco-a-vista' dentro do 'preco'
  for (j = 0; j < qntVista; j++){
    var htmlVista = $(".preco-a-vista:eq("+j+")").html();
    $(".preco:eq("+j+")").append(htmlVista);
  }

  // executa a função de remover os 'preco-a-vista'
  removerVista();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="preco">
R$ 69,90
</div>
<div class="preco-a-vista">
R$ 50,00
</div>

<div class="preco">
R$ 79,90
</div>
<div class="preco-a-vista">
R$ 60,00
</div>

<div class="preco">
R$ 89,90
</div>
<div class="preco-a-vista">
R$ 80,00
</div>

<div class="preco">
R$ 99,90
</div>
<div class="preco-a-vista">
R$ 150,00
</div>

<div class="preco">
R$ 429,90
</div>
<div class="preco-a-vista">
R$ 200,00
</div>
    
25.06.2018 / 19:23
1

Would it be this way?

$('.preco-a-vista').each((i,e) => { $('.preco').eq(i).appendTo($(e).html()); })

In this case it is for equal numbers of div '.preco-a-vista' and 'preco'. There are N ways to do. There we are moving the pr.preco-a-vista div into the pr.br., however, keeping the text of it. If you just want to replace the content, use append.

    
26.06.2018 / 16:59