49

I want to add CSS attributes to my element, but my current solution loses all previous attributes that had an impact on the element.

function checkNr(id) {
  var value = document.getElementById(id).value;
  if (parseFloat(value) == NaN) {
    document.getElementById(id).setAttribute("style", "border:2px solid red; background-color: rgb(255, 125, 115);");
  }
  else {
    document.getElementById(id).setAttribute("style", "border:default; background-color: rgb(255, 255, 255);");
  }
}

Before using this method the element already had the attributes:

float: left;
width: 50px;

Afterwards, the element loses these attributes, leaving only the specific attributes from the JavaScript method. So, I want to add attributes without replacing them.

5 Answers 5

63

Setting the style attribute like that, overwrites the attribute and removes previously set styles.

What you really should do is set the styles directly instead by changing the style property :

function checkNr(id) {
    var elem  = document.getElementById(id),
        value = elem.value;
    if (parseFloat(value) == NaN) {
        elem.style.border = '2px solid red'; 
        elem.style.backgroundColor = 'rgb(255, 125, 115)';
    } else {
        elem.style.border = 'none'; 
        elem.style.backgroundColor = 'rgb(255, 255, 255)';
    }
}
Sign up to request clarification or add additional context in comments.

Comments

5
function checkNr(id) {
    var elem = document.getElementById(id);
    var css = {};
    if (parseFloat(elem.value) == NaN) {
        css = { border: '2px solid red', backgroundColor: 'rgb(255, 125, 115)' };
    } else {
        css = { border: 'none', backgroundColor: 'rgb(255, 255, 255)' };
    }

    Object.assign(elem.style, css);
}

1 Comment

You can't check if the parseFloat call is equal to NaN because NaN is not equal to itself. Use isNan instead.
3

The simplest way for me is:

prev_style=obj.getAttribute("style");
added_style="background-color:red;"
if (prev_style==undefined || prev_style==null) { prev_style="" }
obj.setAttribute("style",prev_style+added_style);

or with jQuery

$(obj).attr("style",prev_style+added_style)

Comments

2

Setting the style attribute of the element directly will overwrite any previous value:

element.setAttribute("style", "border: 2px solid red; /* ... */")
<div id="element" style="float: right; /* ... */">...</div>
<!-- becomes -->
<div id="element" style="border:; 2px solid red; /* ... */">...</div>

Instead, set one property at a time using the element.style.property = value syntax:

const checkNr = id => {
  const element = document.getElementById(id)
  const isNotNum = isNaN(parseFloat(element.value))
  element.style.background = `rgb(255, ${isNotNum ? "125, 115" : "255, 255"})`
  element.style.border = isNotNum ? "2px solid red" : "default"
}

Or, even better, add a conditional CSS class:

const checkNr = id => {
  const element = document.getElementById(id)
  const isNotNum = isNaN(parseFloat(element.value))
  element.classList.add(isNotNum ? "not-number" : "is-number");
}
.not-number {
  background-color: rgb(255, 125, 115);
  border: 2px solid red;
}
.is-number {
  background-color: rgb(255, 255, 255);
  border: default;
}

Comments

-5

jQuery option:

This will add a red border without removing any other inline styles:

jQuery('#element-id').css('border', '1px solid red');

Note: You need to add jQuery as a dependency to your project for this to work.

4 Comments

That's not a JavaScript function, that's jQuery function.
this shouldn't be punished with these amount of negative answers, it is a nice way, maybe it is not javascript, but if someone suggest to edit the answer and add the specification that is JQuery. It could help a lot of people.
this isn't javascript, but this question has been viewed eighty-nine thousand times -- I upvoted as this can definitely help others
jQuery is Javascript. :) How is a a thing made of one thing not the thing it's made up of? @WojciechMaj Anyway, why is the accepted answer with the most votes not at the top of the page?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.