0

This is driving me mad:

// Get the comment template
var comment = $('#CommentTemplate').html();

// Delete all entered text
$("textarea#NewComment").val('');

// Get comments returned ID
var ThisCommentsID = data.substring(2, data.length);
var d = new Date();

// Replace comment variables
comment = comment.replace("{CID}", ThisCommentsID);
comment = comment.replace("{USERNAME}", CurrUsername);
comment = comment.replace("{BODY}", Message);
comment = comment.replace("{GRAV}", GravHash);
comment = comment.replace("{CLEARDATE}", GravHash);
comment = comment.replace("{ISODATE}", ISODateString(d));
comment = comment.replace("{USERURL}", RelURL + CurrUsername);

// Add it
$('#comment-total-wrap').prepend(comment);

This basically works 100% fine, dynamically adding a comment to the list with an AJAX query, but all the values on the template are coming out still as {CID}, {GRAV} etc etc. It's not throwing any errors, the replace isn't working.

I've tried:

comment = comment.replace("{BODY}", Message);

as well but it still just shows as {BODY} on the output HTML. Any help appreciated.

The HTML template on the page is:

<div id="CommentTemplate" style="display:none;">
    <div id="cid{CID}" class="comment-wrapper">
        <div id="CommentHead" class="comment-head ch-highlight">
            <div class="comment-date">
                <abbr class="timeago" title="{ISODATE}" id="Timeago{CID}">{CLEARDATE}</abbr>
            </div>
            <div class="comment-author">
                Written by <a id="CommentAuthorLink" title="Visit this game makers profile" href="{USERURL}">{USERNAME}</a>
            </div>
        </div>   
        <table class="comment-body" width="100%">
            <tr>
                <td width="100" valign="top" align="center">
                    <a id="GravLink" title="{USERNAME} makes games with Construct 2" href="{USERURL}"><img id="GravatarComment" title="{USERNAME}&#39;s Gravatar" class="comment-avatar" src="http://www.gravatar.com/avatar/{GRAV}?r=pg&amp;s=80" /></a>
                </td>
                <td valign="top">
                    <div class="comment-txt">
                        {BODY}
                    </div>
                </td>
            </tr>
        </table>
        <div class="clear"></div>  
    </div>
</div>
2
  • you are using str=str.replace , right as it has to store ...the latest value. Commented Apr 24, 2011 at 22:30
  • It's behaving weirdly, it replaces the body fine, but none of the others. Commented Apr 24, 2011 at 22:33

2 Answers 2

3

Can you please try this

comment.replace("{CID}", ThisCommentsID);

try this

comment= comment.replace(/\{CID\}/gi, ThisCommentsID);
Sign up to request clarification or add additional context in comments.

5 Comments

No luck, doesn't seem to do anything, as you put it throws errors ; expected and enclosed in quotes it doesn't perform any replace action
Perfect! +1!!!!! Could you explain to me why this works please? What was I doing wrong?
@Tom , just with espace sequence we have to tell that string starts and string ends... \{ \} ...
It should be worth mentioning that this fixes the problem because the replace command expects a Regular Expression, and will treat a string as such - which for plain letters tends to work just fine, but the curly braces have special meaning in a regex.
i tried this var output = "1{hi}2"; output.replace("{hi}", "something"); and it worked... go figure
0

just use comment = comment.replace("{BODY}", Message); and it should work.

2 Comments

It doesn't, that's why I asked the question :)
It behaves really weirdly, replaces some of them, other not :(

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.