Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3 using classed() to add and remove class with checkbox

I am having trouble removing a class that I have added using a checkbox. The checkbox is checked to begin with. When it is unchecked by the user it adds a "hideRect" class with classed('hideRect', true); this works great BUT when I check the box again the class doesn't go away.

Here is my code:

this.$node.append('input')
    .attr('type', 'checkbox')
    .attr('checked', true)
    .attr('value', 'med')
    .on('click', () => this.updateRectLine());
  }

private updateRectLine() {

  //var rect = this.$node.getElementsByClassName('.MEDICATION');
  var cbMed = this.$node.attr("checked");
  if (cbMed !== true){
      this.$node.selectAll(".MEDICATION").classed('hideRect', true);
  }

  else if (cbMed == true){
      this.$node.selectAll(".MEDICATION").classed('hideRect', false);
  }

}

thanks in advance!

like image 988
Sockness_Rogers Avatar asked Jul 03 '17 19:07

Sockness_Rogers


People also ask

How do I delete a class in d3 JS?

d3 removeClass To remove a class, the second parameter to classed must be false.

What do the select () and selectAll () functions in d3 do?

select selects the first matching element whilst d3. selectAll selects all matching elements. Both functions take a string as its only argument. The string specifies which elements to select and is in the form of a CSS selector string (e.g. div.

How to append html in d3?

We can add a new HTML element in D3. js by using the append() method. It creates a new HTML element and inserts it at the end of the selected element. Let's create a new div element inside the body tag and add text to it using the text() method.

What is append in d3?

append() function is used to append a new element to the HTML tag name as given in the parameters to the end of the element. If the type that is given is a function then it must be evaluated for each element that is in the selection. Syntax: selection.


1 Answers

You need to update the below function like this

private updateRectLine() { 
  var cbMed = this.$node.select("input[type='checkbox']").prop("checked");
  if (!cbMed)
    this.$node.selectAll(".MEDICATION").classed('hideRect', true);
  else
    this.$node.selectAll(".MEDICATION").classed('hideRect', false);
}

.attr() function only returns the value the checkbox was initialized to, to check for a checkbox's check state, you want to use property checked present on check box elements

like image 134
Meme Composer Avatar answered Sep 28 '22 02:09

Meme Composer