0

Like everyone knows we can style an element using CSS like below

  #a {
     background-color : green;
  }

And using JS

  document.getElementById('a').style.backgroundColor = "green";

But I would like to create some property like background-color and that can be used inside CSS. For example I will create a property like "foobar" then I should be able to use it like below

  #a {
     foobar : value;
  }

Is this possible? I would like to execute some JS on the element that includes this property.

Answers in the question referred to as a duplicate were how to create new CSS and add it to the style sheets using existing CSS properties. But here I am trying to create my own css property...

12
  • Possible duplicate: stackoverflow.com/questions/24885300/… Commented Jan 12, 2015 at 7:32
  • Why do you want to do this? There is probably a better (more standard) way to achieve your goal. Commented Jan 12, 2015 at 7:32
  • @snooze92 What other ways do you suggest? Commented Jan 12, 2015 at 7:33
  • Are you trying to create a string containing the css rules and append it to DOM using javascript? Commented Jan 12, 2015 at 7:33
  • I don't know what is your end-goal. What are you trying to do, ultimately? Commented Jan 12, 2015 at 7:33

3 Answers 3

3

I would like to execute some JS on the element that includes this property.

Although you could just write random properties to the style object:

myElement.style.foobar = "value";

...and that seems to survive on Chrome, Firefox, IE11, even IE8, I would strongly recommend against it.

You'd be much better off using a class or a data-* attribute instead. You can query for them, for one thing.

If you're trying to create your own CSS (something you said in a comment), I would think a data-* attribute would be a good choice.

Using a class:

var myElement = document.createElement('a');
myElement.className = "foobar";
// ...add it somewhere...

// Later
var myElements = document.querySelectorAll(".foobar");
// ...do something with the list...

or with a data-* attribute:

var myElement = document.createElement('a');
myElement.setAttribute("data-foobar", "value");
// ...add it somewhere...

// Later
var myElements = document.querySelectorAll('[data-foobar="value"]');
// ...do something with the list...

querySelectorAll is supported on all modern browsers, and also IE8.

4
  • Hmmm, yeah this is traditional approach. I agree but we will end up always traversing DOM for new elements when we have elements being added dynamically.. Commented Jan 12, 2015 at 7:39
  • @redV: Two responses to that: :-) A) Why would you traverse the DOM when adding new elements? And B) You'd have to do that anyway. Using a data-* or class means you can have the browser's selector engine do it (read: faster), whereas putting it on the style attribute doesn't. Commented Jan 12, 2015 at 7:43
  • For example I added an element which has a class with CSS defined with one of the CSS properties I included manually. Whenever it is added I need to watch DOM for change and add JS on element which contains my CSS property. But I am looking for an inbuilt way that will be triggered whenever an element is added with the class which contains that property. Commented Jan 12, 2015 at 7:46
  • @redV: On modern browsers, you could use mutation observers to do that. On some slightly older ones, the deprecated mutation events (and if you search, I remember seeing a polyfill that provided MutationObserver on older browsers using mutation events to implement it). Other than that, I don't think you have a lot of options. Commented Jan 12, 2015 at 8:18
0

You cannot implement such thing in javascript, while something like you can do in SASS and Less.You have to define a variable with the property e.g. @myblackcolor:#000000;.So this @myblackcolor u can use anywhere in ur css instead of writing again and again color:#ffff.

4
  • saas and less both are compiled back to CSS and they are nothing to do with the above requirement AFAIK. Because in LESS and SAAS you are abstracting the complexity of writing CSS but here I am trying to create own implementation for CSS Commented Jan 12, 2015 at 7:38
  • Then you can do something like if u r doing it in pure javascript var element= document.createElement('a'); element.setAttribute("dom-element", "value"); Commented Jan 12, 2015 at 7:42
  • Please refer to my comments under TJ. Crowder answer. Commented Jan 12, 2015 at 7:47
  • Ohh! He also said the same thing. Enjoy if u got any help. If not find alternate bro.Why u want to make it complex when easy ways are out there to do it. Commented Jan 12, 2015 at 7:50
0

Why don't you define your classes in your stylesheet and assign them dynamically to newly created elements via JavaScript based on some conditions? That way you wouldn't need any DOM traversal. E.g.:

/* in your stylesheet */
.classOne{
    font-weight: bold;
    background-color: #808080;
}

.classTwo{
    width: 100%;
    background-color: #000000;
}


//then in your JavaScript
var elt = document.createElement("button");
if(condition a){
    elt.className = "classOne";
    //other scripts related to elt given class name is classOne
}else if(condition b){
    elt.className = "classTwo";
    //other scripts related to elt given class name is classTwo
}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.