0

In the following Javascript, I would like to replace all text beginning with "#" to become a link and change the color. The function works fine, but it changes all of the descriptions on the page instead of each photo individually. Is there a way to have it work for each photo in the for loop?

Javascript:

var str = $('.description').html();
var edt = str.replace(/(^|\s)(#[a-z\d-]+)/ig, "$1<a href='{% url "timeline" %}'><span style='color: blue;'>$2</span></a>");
$('.description').html(edt);

Django:

{% for photo in photos %}
    <div class="photo-item">
        <p class="description">{{ photo.description }}</p>
    </div>
{% endfor %}

Thank you in advance!

1 Answer 1

1

In your Javascript, $('.description') returns a list of all the elements that have a class description, while the html() function

  • returns the content of the FIRST element when reading
  • modifies EACH element when updating

What you need is something like this (Not tested):

$('.description').each(function(index) {
  var str = $(this).html();
  var edt = ...;
  $(this).html(edt);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.