-2

Can I make a javascript function so that the property and value would work in stylesheet?

So the following would work:

.selector{
   myproperty: myvalue1; /* myvalue1, myvalue2, myvalue3 */
}

Suppose, I would define the myvalue1, myvalue2, and myvalue3 in js for eg. sets element's background-color to red, green and blue respectively and now defining it in stylesheet it should be able to set div's background color to red,green, and blue at their respective.

if('myproperty' in document.body.style){
  //don't do anything
}else{
    //do stuff here
    //something like this
    document.body.style.myproperty = [value1,value2,vlaue3]
}
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
  • do you mean injecting css stylesheet as string using javascript? – Stefano Ortisi Jul 22 '14 at 10:52
  • I don't think you can add your own custom css properties – Kyle Emmanuel Jul 22 '14 at 10:52
  • Can you give an example of 'myproperty'? – sree Jul 22 '14 at 10:52
  • 1
    I'd have thought you'd be able to ask a question a little better as a 10K+ user. What is your issue here? As far as I can tell you want to add some styles to a style sheet; what does this have to do with JavaScript? – George Jul 22 '14 at 10:52
  • @oGeez I wanted to know that could I make my own custom css properties... – Bhojendra Rauniyar Jul 22 '14 at 10:53
  • That would do what? Correspond to other, *real* properties? – George Jul 22 '14 at 10:54
  • What good is a custom css property if no browser knows how to render it? – Cameron Jul 22 '14 at 10:54
  • What does it have to do with javascript? Do you want to be able retrieve the value of your custom property using javascript? – Cameron Jul 22 '14 at 10:56
  • @Cameron yes, I want to define my custom property and value so that I can use later it in stylesheet... – Bhojendra Rauniyar Jul 22 '14 at 11:00
  • By the sounds of it you are wanting to do something impossible... I'm still unsure exactly how to help, sorry. Perhaps you should check out sass/scss http://sass-lang.com - it has some pretty nifty features such as variables and mixins which might help you. – Cameron Jul 22 '14 at 11:09
  • @oGeez yes, you are right. As other style property I want to create my own .... – Bhojendra Rauniyar Jul 22 '14 at 11:10
  • So you would like JavaScript to grab your stylesheet, parse it, and map your custom properties to real ones? – George Jul 22 '14 at 11:12
  • @C-linkNepal you could probably benefit from learning sass then by the sounds of it. Check out the "variables" in the guide here http://sass-lang.com/guide – Cameron Jul 22 '14 at 11:15

1 Answers1

1

Your question is confusing, but you might be able to use jQuery's .css function.

$('.selector').css('myproperty','myvalue1');
Cameron
  • 597
  • 1
  • 4
  • 14