mardi 28 août 2018

change class in javascript by d3 select and filter

I have a function like below.

First, I change all class to "badge badge-secondary". Then, check text in span. If it is "right" and clicked, then change to "badge badge-primary". Else, it is "wrong" and clicked, changing to "class", "badge badge-danger".

Can I make my code more concise and righter?

function updateOrderType(ansType) {
    lastAnsType = ansType;
    var ansBadge = d3.select("#anstype").selectAll("span.badge");
    ansBadge.attr("class", "badge badge-secondary");
    ansBadge.filter(function() {
        if (d3.select(this).text() == ansType) {
            return d3.select(this).text() == "right";
        }
    }).attr("class", "badge badge-primary");
    ansBadge.filter(function() { 
        if (d3.select(this).text() == ansType) {
            return d3.select(this).text() == "wrong";
        }
    }).attr("class", "badge badge-danger");
}

Aucun commentaire:

Enregistrer un commentaire