1

Is it possible I can replace my name with "anonymous" via jquery. but still keep my anchor tag with the specific link to a comment. I couldn't figure it out so I tried removing the text and keeping the comment with

$('div.entryFooter').replaceWith($('div.entryFooter a')); 

but it showed all the content from all three 'a' elements in all of my divs.

Ideally, I just need the name replaced with "anonymous". My name will always be different so I need a way to find the name after "Posted by"

<div class="entryFooter">Posted by mjroodt at 24/10/2011 11:48<a href="/BlogPage.aspxid=20396&blogid=58906">Comments (1)</a></div>

<div class="entryFooter">Posted by mjroodt at 27/10/2011 13:33<a href="/BlogPage.aspx?id=12396&blogid=58945">Comments (2)</a></div>

<div class="entryFooter">Posted by mjroodt at 27/10/2011 15:59<a href="/BlogPage.aspx?id=14396&blogid=59963">Comments (7)</a></div>

Many thanks

2
  • 1
    Are you aware of the fact that viewing the original HTML source will still simply reveal the original author? Commented Oct 25, 2011 at 16:39
  • @pimvdb, yes I am aware that the original author will be visible through the original HTML source but unfortunately I only have access to the client side. Commented Oct 25, 2011 at 18:28

6 Answers 6

4

No need for jQuery here. Just use the Javascript replace([substring], [newstring]) function:

var value = $(".div.entryFooter").text().replace("James Johnson", "Anonymous");

Should be noted that this is only for the visual display. If you don't want the names to show at all, you'll need to parse them out at the server or database level.

$(".div.entryFooter").each(function(){
    $(this).text($(this).text().replace("James Johnson", "Anonymous"));
});
Sign up to request clarification or add additional context in comments.

6 Comments

Um..that is jQuery. Edit: well, really, jQuery is only used to retrieve the node from the DOM.
The replace function is not.
You can do it all in once shot as well. $(".div.entryFooter").text( $(".div.entryFooter").text().replace("James Johnson", "Anonymous") );
@Justin Carlson: That fetches the text of the first element and sets all elements' text to that, so it's probably not what the OP wants.
it the best solution, but only if you know the name
|
3

This loop will parse out the name, whatever it might be, and replace it with in this case anon:

$('div').each(function () {
   var $this = $(this),
    text = $this.text(),
    name = text.match(/Posted by (.+) at .+/)[1]
   $this.text(text.replace(name, 'anon')) 
});

Example

Using:

Comments

2

You can do this by using a few lines of Jquery below. This solution will keep your anchor tags working as they were and you don't need to know the name you are trying to replace.

$('.entryFooter').each(function(){
    var html_arr = $(this).html().split(' ');
    html_arr[2] = 'anonymous';
    $(this).html(html_arr.join(' '));
});

EXAMPLE

Comments

2

If you want to hide your real name, you have to adjust the server response.
For visual changes only, use the code below:

var name = "mjroodt";
var replaceBy = "anonymous";

//Generate a replace-all pattern
var pattern = new RegExp(name.replace(/([[^$.|?*+(){}])/g, '\\$1'), 'g');

//Loop through each `div.entryFooter` element
$("div.entryFooter").each(function(){
    var $this = $(this);
    var text = $this.text();
    //Check if the name exists. Otherwise, ignore
    if(text.indexOf(name) == -1) return;
    $this.text(text.replace(pattern, replaceBy));
})

Comments

1

You could match any name and replace it with "anonymous" using the below code,

text.replace(/by[ ][a-zA-Z0-9]*[ ]at/,"by anonymous at");

The above would replace the content between "by" and "at" in the sentence "posted by xyz at" by "posted by anonymous at".

1 Comment

I think the character class for just one space is not necessary.
0

I'd suggest you to wrap text into span and iterate through them to change text() with RegExp:

see the work example for you requirements: http://jsfiddle.net/mikhailov/ypRsP/

HTML

<div class="entryFooter">
    <span>Posted by mjroodt at 24/10/2011 11:48</span>
    <a href="/BlogPage.aspxid=20396&blogid=58906">Comments (1)</a>
</div>

<div class="entryFooter">
    <span>Posted by mjroodt at 27/10/2011 13:33</span>
    <a href="/BlogPage.aspx?id=12396&blogid=58945">Comments (2)</a>
</div>

<div class="entryFooter">
    <span>Posted by mjroodt at 27/10/2011 15:59</span>
    <a href="/BlogPage.aspx?id=14396&blogid=59963">Comments (7)</a>
</div>

JS

$('.entryFooter span').each(function(){
    var that    = $(this);
    var content = that.text();
    var t = content.replace(/^Posted by [A-Za-z]+/, "Posted by Anonymous");
    that.text(t);
});

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.