What I'm doing wrong.
Need to add CSS with JavaScript but I have to keep all the inline styles.
In this case when user clicks h1 element, the color of the inline style needs to stay the same and just add new style from javascript.
Here is fiddle: https://jsfiddle.net/59qfxwcu/
<h1 style="color: red;">Test</h1>
<h1 style="color: green;">Test</h1>
<script>
function some(){
var element = document.querySelectorAll('h1');
element.forEach(function(item){
item.onclick = function(){
var inlineCss = this.style;
item.style.cssText = inlineCss + 'background: blue; font-size: 12px;';
}
});
}
some();
</script>
this.styleis not a text , it is an object. You must use your debugger by yourself first+=instead of=?