Each does not work

Asked

Viewed 97 times

1

I’m looking to replace the tag <a> for <span>.

I tried to do it like this, but it doesn’t work:

$(document).ready(function() {
  $('#lista-noticias .box-noticia .conteudo-noticia').each(function() { 
    alert("teste");
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="lista-noticias">       
  <div class="box-noticia">
    <div class="conteudo-noticia">
      <a href="qualquer.coisa1.html">
    </div>
  </div>
  <div class="box-noticia">
    <div class="conteudo-noticia">
      <a href="qualquer.coisa2.html">
    </div>
  </div>
  <div class="box-noticia">
    <div class="conteudo-noticia">
      <a href="qualquer.coisa3.html">
    </div>
  </div>
  <div class="box-noticia">
    <div class="conteudo-noticia">
      <a href="qualquer.coisa4.html">
    </div>
  </div>
</div>

  • 1

    Try to close your tags <a> to see if it doesn’t work - </a>

  • How many alerts does the code give?

  • @Brunodm, I turned your code into a snippet, and it ran without modification.

  • There is no Alert. No each, I need to put the path of all tags ? Or could I put it like this: $('#newslist . contentudo-noticia'). each(Function() {...

3 answers

2


Assuming that your tags <a> are closed with the corresponding </a> and that have some text, so you can use the .replaceWith() jQuery and do so:

$('#lista-noticias .box-noticia .conteudo-noticia a').replaceWith(function() {
    return $('<span/>', {html: this.innerHTML});
});

jsFiddle: https://jsfiddle.net/qevg0cz7/

This method replaces all elements of the selector with the return of the iterator function.

0

The code works the way it is written (see below):

https://jsfiddle.net/ds4duhzf/

Check that there are no errors in your Browser Console, particularly that jQuery is being included correctly.

0

   $('#lista-noticias .box-noticia .conteudo-noticia').each(function() { 
          $(this).find("a").replaceWith("<span>abcd</span>");
        });

Follows: https://jsfiddle.net/4gj5subt/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.