How to select gradparent of an css element using JQuery?

Hi this

This is my JQuery code

My Requiremnet Text field should be enable or disable whethar checkbox on left side to them is checked or not

When you click the checkbox, you will want the selector to start looking from the element. That can be done with:

$("...", $(this).parents("tr"));

which looks for the selector in the first parameter, that exists within the second.

2 Likes

You can indeed do this using .closest(), but to get the corresponding text input elements, you have to go back down the DOM using .find(), like

$(".selectCategory").on("click", function(){
  var $this = $(this);
  
  $this
    .closest('tr')
    .find('input[type="text"]')
    .attr('disabled', !$this.prop('checked'));
});

PS: You can do this more efficiently if those elements are all children of the same node, i.e. if you don’t use tables for styling (which you should avoid anyway!). Like

$this
  .nextAll('input[type="text"]')
  .attr('disabled', !$this.prop('checked'));
2 Likes

although this has solved my problem of enable and disable

but there is one issue if I have typed some value in text and uncheck checkbox that value
is still there in input type I want that value should be blank when checkbox in unchecked

Just add .val('') to clear the value.

2 Likes

Thanks

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.