Search code examples
javascriptunderscore.jsunderscore.js-templating

remove html element from the template in underscore.js


My JSON response is something like this:

{
          "description": "discription about the feaure will come here.  <a href=\"http://example.com\" target=\"_blank\">Learn more</a>",
          "largeIconURL": "some_path/ico_someicon.gif",
          "displayName": "feature one"
        },
        {
          "description": "discription about the feaure will come here.",
          "largeIconURL": "some_path/ico_someicon.gif",
          "displayName": "feature two"
        },
        {
          "description": "discription about the feaure will come here. ",
          "largeIconURL": "some_path/ico_someicon.gif",
          "displayName": "feature three"
        },

My template is something like this

<%
        _.each(items,function(item, key, list){
      %>
      <li>
        <div class="oneSpec">

          <div class="description">
            <h4><%= item.displayName %></h4>
            <p><%= item.description %></p>
          </div>
        </div>
      </li>

      <%
        });
      %>

How can i remove the anchor tag from description node before painting it to the DOM


Solution

  • I'm not entirely sure what you mean by "from description", but you can conditionally include something by using <% if (condition) {%>. I'm just guessing what you mean. If this is not it, you have to be more clear...

    Update:

    Remove the anchor like so (Regex in Javascript to remove links):

    <%=item.description.replace(/<a\b[^>]*>(.*?)<\/a>/i,"")) %>