Jquery replace in html string 2 times separatly
<p>So i have a html template: </p>

<pre><code>      &lt;div style='background-color: red;'&gt;
          &lt;div&gt;
                $QuestionUpButton
              &lt;%--&lt;img style="cursor: pointer" onclick="onAddQUestionVote" src="../../Images/arrow-up-24-ns.png" data-questionvoteupbutton="$i" id='addQuestionVote' alt='Vote up' data-questionid='$questionUid' /&gt;--%&gt;
              &lt;span data-questionvotegroup='$i'&gt;$QuestionVote&lt;/span&gt;
              &lt;span data-questiontextgroup='$i'&gt;$textboxText&lt;br /&gt;
              &lt;/span&gt;
          &lt;/div&gt;
          &lt;div id="mm" style='background-color: white;'&gt;
              $AnswerUpVote
              &lt;%--&lt;img style="cursor: pointer" onclick="onAddAnswerVote" src="../../Images/arrow-up-24-ns.png" data-answervoteupbutton="$i" id='addAnswerVote' alt='Vote up' data-answeruid='$answerGuid' /&gt;--%&gt;
              &lt;img style="cursor: pointer" onclick="onRemoveAnswerVote" src="../../Images/arrow-down-24-ns.png" data-answervotedownbutton="$i" id='removeAnswerVote' alt='Vote down' data-answeruid='$answerGuid' /&gt;
              &lt;span data-answervotegroup='$i'&gt;$AnswerVotes&lt;/span&gt;
              &lt;span data-labelgroup='$i'&gt;Answer : &lt;/span&gt;
              &lt;span data-answertextgroup="$i" data-questionid='$questionUid' data-answerid="$answerGuid"&gt;$AnswerSpan&lt;/span&gt;
              &lt;input type='button' id='editButton' data-editlinkbuttongroup="$i" value='Edit' /&gt;
              &lt;input type='button' id='deleteButton' data-deletelinkbuttongroup="$i" value='Delete Question' /&gt;
          &lt;/div&gt;

      &lt;/div&gt;
</code></pre>

<p>on document load i run code where i replace all $ variables like this: </p>

<pre><code>function MyFunction(result) {
    //console.log(result);
    var panel = $('#mypanel');
    var userGuid = GetUserGuid().d;
    var panel = $('#MainContent_AnsweredQuestions_mypanel');

    for (var k = 0; k &lt; result.d.length; k++) {
        var notVotedQuestion = "&lt;img style='cursor: pointer' onclick='onAddQUestionVote' src='../../Images/arrow-up-24-ns.png' data-questionvoteupbutton='" + i + "' id='Img1' alt='Vote up' data-questionid='" + result.d[k].Uid + "' /&gt;";
        var notVotedQuestion1 = "hulabula";
        var notVotedAnswer = "notVotedAnswer";
        var votedAnswer = "";

        console.log(result.d[k].Uid)
        var html = $("#OwnerTemplateQuestionWithoutAnswer").html().replace(/\$i/g, i)
            .replace(/\$AnswerSpan/g, result.d[k].Answers[0].Conetent)
            .replace(/\$answerGuid/g, result.d[k].Answers[0].Uid)
            .replace(/\$questionUid/g, result.d[k].Uid)
            .replace(/\$QuestionVote/g, result.d[k].QuestionVotes.length)
            .replace(/\$AnswerVotes/g, result.d[k].Answers[0].AnswerVotes.length)
            .replace(/\$textboxText/g, result.d[k].Content);

        //var html1 = $("#OwnerTemplateQuestionWithoutAnswer").html(html);

        if (didUserVote(result.d[k], userGuid) == false){
            html.html().replace("$AnswerUpVote", notVotedQuestion); 
        }

        panel.append(html);
        i++;


    }

}
</code></pre>

<p>part where i replace first time is working great and exactly how i want:</p>

<pre><code> var html = $("#OwnerTemplateQuestionWithoutAnswer").html().replace(/\$i/g, i)
                .replace(/\$AnswerSpan/g, result.d[k].Answers[0].Conetent)
                .replace(/\$answerGuid/g, result.d[k].Answers[0].Uid)
                .replace(/\$questionUid/g, result.d[k].Uid)
                .replace(/\$QuestionVote/g, result.d[k].QuestionVotes.length)
                .replace(/\$AnswerVotes/g, result.d[k].Answers[0].AnswerVotes.length)
                .replace(/\$textboxText/g, result.d[k].Content);
</code></pre>

<p>but later in the code i need to load image differently depends on some conditions</p>

<pre><code>   if (didUserVote(result.d[k], userGuid) == false){
        html.html().replace("$AnswerUpVote", notVotedQuestion); 
    }
</code></pre>

<p>and this part of cide doesnt work at all... Any suggestions on how to solve this, or how i can improve replacement in some kind of a way??</p>
