4

I have this code right here:

var tweetText = $("#tweet_text").text();
var replaced = replace(tweetText);

document.getElementById("tweet_text").innerHTML = replaced;

It gets text from a text element, and highlights the mentions and URLS to make them clickable. Here is the replace() code:

function replace(text) {
    return text.replace(/([@#])([a-z\d_]+)/ig, function(_, marker, tag) {
        if (marker === "@")
            return '<a href="?r=site/twitterprofile&q=$1">' + "@" + tag + '</a>';
        return '<a href="?r=site/hashtag&q=$2">' + "#" + tag + '</a>';
    });
}

I have a long list of divs / tweets, and when I run replace() it only applies the pattern matching function to one div. To get around this, I thought of collecting all of the #tweet_textdivs using .each(), and then applying the replace() function once it loops through.

Could somebody help me do that please?

2
  • see the doc for each Commented Jul 22, 2015 at 4:19
  • You want to change value of text box or content of the div? Commented Jul 22, 2015 at 4:31

2 Answers 2

5
$('.your-tweet-class').each(function() {
    $(this).text( replace( $(this).text() ) );
});

You can remove the spaces, I just put them in for clarity.

Sign up to request clarification or add additional context in comments.

1 Comment

i ended up using .html() because I was echoing html and not text. Everything works fine now though :)
1

You can use html() as follow.

Set the HTML contents of each element in the set of matched elements.

$('.tweetClass').html(function(index, oldHtml) {
    return replace(oldHtml);
});

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.