Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I'm trying to set multiple attributes in d3 but I get an error for the stroke width attribute due to the dash.

I can get around this by chaining another attribute's call but I would like to keep them all in one call if I can.

Is this possible?

var edges = svg.selectAll("line")
        .data(data.edges)
        .enter()
        .append("line")
        .attr("stroke", "#ccc")
        .attr("stroke-width", 3};
share|improve this question

closed as off-topic by ChrisWue, Manny Meng, syb0rg, Jamal Dec 27 '14 at 0:57

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." – ChrisWue, Manny Meng, syb0rg, Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.

    
My apologies about the off topic. Should have asked on stackoverflow. –  canyon289 Dec 27 '14 at 15:07
    
I don't think that it's off topic at all. It contains working code, and asks for a better way to do the same thing. –  Guffa Dec 27 '14 at 15:23
    
"Is this possible?" does not make an on-topic Code Review question. "This seems to work, but is it a good idea?", on the other hand, would be a reasonable question. –  200_success Dec 27 '14 at 21:47

1 Answer 1

up vote 1 down vote accepted

You can just use a string literal as identifier to use the object literal syntax. Note also that there is a colon after the identifier, not a comma:

var edges = svg.selectAll("line")
    .data(data.edges)
    .enter()
    .append("line")
    .attr({
        stroke: "#ccc",
        'stroke-width': 3
    });
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.