The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

SSteve (24) [Avatar] Offline
#1
In section 3.2.3, the line ".classed(function(p) { return p.region == d.region ? "active" : "inactive"}, true);" gives me the error "TypeError: 'undefined' is not a function (evaluating 'n.trim()')"

If I replace that line with ".classed("active", true)" all the circles turn red so I think all the code around it is correct.

I replaced it with ".classed( function (p) { return "active";}, true);" and still get the same error. I added "console.log(p);" before the return statement and nothing gets sent to the log so the error is happening before the function is ever called.

Is there something I'm doing wrong? My JavaScript experience is pretty minimal so I'm not sure how to troubleshoot this.
SSteve (24) [Avatar] Offline
#2
Re: Having trouble getting code in section 3.2.3 to run
For what it's worth, replacing that line with ".attr("class", function (p) { return p.region == d.region ? "active" : "inactive"});" make things work.
elijah.meeks (127) [Avatar] Offline
#3
Re: Having trouble getting code in section 3.2.3 to run
Thanks for catching this, Steve. I'm not sure how I let that sneak in there. First, the proper code:

 .each(function(d,i) { p.region == d.region ? 
d3.select(this).classed("active",true) : d3.select(this).classed("inactive",true)}) 


My JavaScript-fu is not strong enough to understand why I can't use that function inline like it is in the code that doesn't work, but replacing it with a .each() that makes the selection and class change explicitly achieves exactly the same result with a bit more code.

As for why to use .classed() instead of .attr("class"), remember that the latter will overwrite whatever classes are associated with your selected elements, whereas .classed() allows you to add or remove certain classes.