So I have made a plugin for my website using javascript, one line of the code is output.innerHTML = "Test";
Can I style this using CSS or is there another way?
So I have made a plugin for my website using javascript, one line of the code is output.innerHTML = "Test";
Can I style this using CSS or is there another way?
Can I style this using CSS
Yes. Write a selector that matches the element you have a reference to in output.
Alternatively, add new elements inside it and write selectors that match them.
or is there another way?
Not anything sane.
you can write directly like this
output.innerHTML = "<p style='your styles'>Test</p>";
If you want to style the text which is appended to the output element, then either apply a CSS class or edit the style via javascript, for example by doing the following:
output.style.color = "#FF0000";
which would produce red text.
the html
<html>
<body>
<div id="output"></div>
</body>
</html>
and javascript
window.onload = function(){
var output = document.getElementById('output');
console.log(output);
output.innerHTML = "test";
output.style.color ="#ff0000";
};
will do the work. CODEPEN
Be noted to always use window.onload or $(document).ready(function(){}) for the jQuery equivalence to make sure that the DOM element has exist by the time your javascript code is being executed.