0

I have code which allows adding a class to 'style' section.

function appendCssClassToStyle(clas, prprty, val)
{
  $('#myStyle').append('.' + clas + '{' + prprty + ':' + val + ';}');
}

This code adds the class many times. The problem is that I would like to be able to add a class only if it does not exist in the styles section yet. How can I do this? Thanks for help !

Pat
  • 117
  • 1
  • 9

3 Answers3

0

I think you should read up on the use of jQuery and how inline styles are added to html elements...

inline style:

<div class="someEl" style="background-color:#ff0000;">Some awesome text</div>

This element has a class .someEl which you can style in a CSS stylesheet:

.someEl {
    background-color:#fff; /* setting bg color to white */
    color:#000; /* setting font color to black */
}

Above would result in a div with a red (#ff0000) background since inline styles will overwrite styles from stylsheets.

With jQuery you can add and remove inline styles from the element. Adding inline styles:

$('.someEl').css({
   'color': '#fff',
   'backgroundColor': '#ff0000'
});

Removing inline styles:

$('.someEl').css('backgroundColor', '');
Brainfeeder
  • 2,604
  • 2
  • 19
  • 37
0

You could use something like this:

$('.element:not(.class-name)').addClass('class-name');

as mentioned here https://stackoverflow.com/a/33265116/2469264

Lazaro Henrique
  • 527
  • 5
  • 7
0

You can try this

function appendCssClassToStyle(clas, prprty, val)
{
  //var prprty = 'text-align';
  //var val = 'center';
  $('.' + clas).css(prprty,val);
}
Sarvap Praharanayuthan
  • 4,212
  • 7
  • 47
  • 72