-2

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?

user3095871
  • 1
  • 1
  • 3

4 Answers4

1

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.

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
0

you can write directly like this

output.innerHTML = "<p style='your styles'>Test</p>";
ɹɐqʞɐ zoɹǝɟ
  • 4,174
  • 3
  • 18
  • 35
0

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.

chucktator
  • 1,539
  • 9
  • 15
-1

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.

mcn
  • 721
  • 4
  • 9
  • Thanks for that I have tried out a few methods mentioned here but have decided to style using #output in a separate css file. I wondered what windows.onload is about as have seen it a few times thanks for explaining what it does! – user3095871 May 04 '14 at 14:25
  • `window.onload` waits for the HTML element to be exist (and all images) in the page before the code executes. This prevents you from getting the `undefined` or `null` value from `document.getElementById('output')`. [READMORE](http://stackoverflow.com/questions/3698200/js-window-onload-vs-jquery-document-ready) – mcn May 04 '14 at 15:53