Search code examples
javascriptjqueryappendrowappendchild

How to use a function as a condition for if in JS/JQ


Here is my code. In this if my row Key is Grand Total that should not be give me a link.

row.append("td")
  .append('span')
  .classed({ '2017': true })
  .attr('href', function(item) { 
    if (item.key != "Grand Total") 
      return (projectBaseUrl + item.key + "2017"); 
  })
  .append("a")
  .attr('target', "_top")
  .text(function(item) { 
    return item.key; 
  });

But if I give for append like this .append(function(item) { if (item.key!="Grand Total") return ("a");}) It is not executing. It throws an error as Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'. TypeError: Failed to execute 'appendChild' on 'Node': parameter So what I can do for this.


Solution

  • row.each(function(item) 
            {
             var self = d3.select(this);
            if (item.key!="Grand Total") 
                {
                    self.append("td").append('span')
                .classed({'2017': true})
                .append("a")
                .attr('href', function(item) {return (projectBaseUrl+item.key+"2017");})
                    .attr('target', "_top")
                .text(item.2017);
                }
            else 
                {
                        self.append("td").append('span')
                .classed({'2017': true})
                .text(item.201717);
                }
            });
    

    This helps me out from problem. Thanks for your input guys