Question:
I am trying to get a search filter on checkbox. so everytime a checkbox is clicked the result will be displayed accordingly. So to show the result, I am currently clearing the page with $('#editor-master').html('');
where editor-master is the outermost element. and I have an empty similar html piece inside <script=text/template>
so as u all might know browser will ignore this content. And so, for my result I clone this piece of html code like below:
$newTemplate = $template.clone();
$newTemplate will have:
<div class="featured-box featured-box-primary">
<div class="box-content">
<div class="editor">
<div class="row">
<div class="col-md-12">
<div class="col-md-9 text-left">
<h4 id="username||id||" value=""></h4>
<i class="icon icon-star"></i>
<i class="icon icon-star"></i>
<i class="icon icon-star"></i>
<i class="icon icon-star"></i>
<i class="icon icon-star-o"></i>
<p class="ratings">
</p>
</div>
<div class="col-md-3" id="||editor_id||">
<span id="favorite||id||" class="userstar user-favorite-stars"></span>
<span class="usericon"><i class="icon icon-user"></i></span>
</div>
</div>
</div>
<div class="row margin-bottom-10">
<div class="col-md-12">
<dl>
<dt>Credentials</dt>
<dd class="margin-bottom-10" id="credentials||id||"></dd>
<dt>Specialties</dt>
<dd class="margin-bottom-10" id="editor_specialties||id||"></dd>
<dt>No of Words</dt>
<dd class="margin-bottom-10"></dd>
<dt>Description</dt>
<dd id="self_description||id||"></dd>
<!--dt>Approval Date</dt>
<dd id="approval_date||id||"></dd-->
</dl>
<div class="col-md-12 margin-top-10">
Unavailable until
</div>
<div class="col-md-6 margin-top-10">
<a href="" class="btn btn-primary btn-sm" id="submit_doc||id||">Submit a Document</a>
</div>
<div class="col-md-6 margin-top-10">
<a href="" class="btn btn-primary btn-sm">Free Sample Edit</a>
</div>
</div>
</div>
</div>
</div>
</div>
I am trying to find all the id attributes which will match ||id|| and replace that with a number which is incremented in the loop, and also ||||editor_id||||
so far I have tried,
$target = $("#editor-master");
$newTemplate.appendTo($target);
$target.replace(/||id||/g,j);
$target.replace(/||editor_id||/g,value.editor.id);
$target.find('h4').text(value.username);
and
$target = $("#editor-master");
$target.append($newTemplate.toString().replace(/||id||/g, j));
Which won't work. Thanks.
Edited to answer the question by "sb":
First stringify the html, replace the matched string and revert that back to original html: so here $target.append(t)
is the html content I was looking for.
var t = $newTemplate.get(0).outerHTML;
t = t.replace(/\|\|id\|\|/g,j);
t = t.replace(/\|\|editor_id\|\|/g,value.editor.id);
//append to target, which will render the html with replaced values.
$target.append(t);