Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove style attribute added with jquery

I am using a devExpress table with some custom requirements.

(UPDATE) Took a break from this for a day and went back and did it properly using React Styling! Thanks for suggestions

SCREENSHOT

In the screenshot I have certain cells disabled. However the user wants all cells to look disabled other that the row selected.

Using this

   window
  .$("td")
  .not(document.getElementById(this.state.selection[0]))
  .not(document.getElementsByClassName(this.state.selection[0]))
  .not("td:first-child")
  .not(window.$("td:contains('iPlay')"))
  .not(window.$("td:contains('iLOE')"))
  .not(window.$("td:contains('iInvest')"))
  .not(window.$("td:contains('SPACER')"))
  .not(window.$("td:contains('$MM')"))
  .not(window.$("td:contains('$/BOE')"))
  .attr("style", "color:#868a8f");
window
  .$("td > div > div > div > input")
  .not(document.getElementsByClassName(this.state.selection[0]))
  .attr("style", "color:#868a8f");

I managed to achieve my desired result on page load SCREENSHOT2

My problem is when I select a new row I cannot remove that color I applied before when it was not selected. I am trying to use "has" to find the selected row and change the color back to inherit or completely remove the style attribute.

    window
  .$("td")
  .has(document.getElementById(this.state.selection[0]))
  .has(document.getElementsByClassName(this.state.selection[0]))
  .not("td:first-child")
  .not(window.$("td:contains('iPlay')"))
  .not(window.$("td:contains('iLOE')"))
  .not(window.$("td:contains('iInvest')"))
  .not(window.$("td:contains('SPACER')"))
  .not(window.$("td:contains('$MM')"))
  .not(window.$("td:contains('$/BOE')"))
  .attr("style", "color:inherit");
window
  .$("td > div > div > div > input")
  .has(document.getElementsByClassName(this.state.selection[0]))
  .attr("style", "color:inherit");

If it helps I do have the ids of the rows that are NOT selected. I tried to do something with that but did not have any luck

  const otherRows = ExpensesUtils.ROW_PROPS.filter(x => x !== this.state.selection[0]);
for (let i = 0; i < otherRows.length; i += 1) {
  window
  .$("td")
  .has(document.getElementById(otherRows[i]))
  .has(document.getElementsByClassName(otherRows[i]))
  .attr("style", "color:inherit");
  window
  .$("td > div > div > div > input")
  .has(document.getElementById(otherRows[i]))
  .has(document.getElementsByClassName(otherRows[i]))
  .attr("style", "color:inherit");
}

link to HTML Table HTML

this.state.selection[0] is the selected rowId from the list below

I have applied the the rowIds to classes in the nested components. I could not figure out another way to access them.

  const ROW_PROPS = [
  "leaseAndWellExpense",
  "leaseAndWellExpenseBoe",
  "iloeLeaseAndWellExpense",
  "iloeLeaseAndWellExpenseBoe",
  "gnaLeaseAndWell",
  "gnaLeaseAndWellBoe",
  "transportation",
  "transportationBoe",
  "divisionGnA",
  "divisionGnABoe",
  "gatheringProcessing",
  "gatheringProcessingBoe",
  "hqGnA",
  "hqGnABoe",
  "interestExpense",
  "interestExpenseBoe",
  "netProdBoe",
  "leaseImpairments",
  "leaseImpairmentsBoe",
  "ddaProducing",
  "ddaProducingBoe",
  "iInvestDdaProducing",
  "iInvestDdaProducingBoe",
  "ddaGatheringProcessing",
  "ddaGatheringProcessingBoe",
  "iInvestDdaGatheringProcessing",
  "iInvestDdaGatheringProcessingBoe",
  "marketingCosts",
  "otherIncomeExpense",
  "otherIncomeExpenseBoe",
  "otherRevenue",
  "incomeTaxProvision",
  "incomeTaxProvisionBoe",
  "severanceTaxes",
  "severanceTaxesPercent",
  "currentTaxes",
  "currentTaxesRate",
  "netWellHeadRevenue",
];
like image 794
texas697 Avatar asked Jan 18 '21 14:01

texas697


People also ask

How we can remove CSS in jQuery?

2 different approaches to remove style inline CSS in jQuery. Use removeAttr() to remove all inline style attribute. Use CSS() to remove the specific CSS property.

How do I remove a style from a specific element?

You can use "initial" or "unset" but you have to manually apply them for each property, and what is even worse, they will not return properties to the element's default display values as set by each browser's default UA style sheet, but "initial" will essentially erase the element's property values and create a ...

What jQuery method is used to completely remove attributes from elements?

To remove all attributes of elements, we use removeAttributeNode() method.

How do I remove a single style property in CSS?

Setting the value of a style property to an empty string — e.g. $('#mydiv'). css('color', '') — removes that property from an element if it has already been directly applied, whether in the HTML style attribute, through jQuery's . css() method, or through direct DOM manipulation of the style property.


Video Answer


2 Answers

The easiest way of doing this is by creating a CSS rule's stylesheet.

In that stylesheet, you should define 2 classes.

Let's suppose 1 for your desired CSS rules and the other for the default/none rules.

I am just showing you the simplest version of doing this thing but with another aspect.

$('#b1').on('click', function() {
  $('.c1').removeClass('c1');
  $(this).addClass('c2');
});
.c1 {
  color: red;
}

.c2 {
  color: green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button id="b1">Change</button>
<p class="c1">This is a Test Line.</p>
like image 178
John Doe Avatar answered Oct 28 '22 23:10

John Doe


The easiest way is

 $('#idName').on('click', function()
{
    $('.className').removeClass('removeClassName');
    $(this).addClass('addClassName');
});

The code above means that when a button with the id of IdName is clicked, the element with className will be removing the class of removeClassName, and adding the class of addClassName.

For further clarification you can have a look at Removing CSS Using JQuery Documentation

like image 35
Tan Yi Jing Avatar answered Oct 28 '22 23:10

Tan Yi Jing