Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add style to content in JavaScript?

I have been working in a JavaScript file and my content has been working with phrases. Now I want to change the style of those phrases. The first function (see function swapFE) I want to change the font style of the phrase node to normal. And change the color of the phrase node to the color value (155, 102, 102). The second function (see swapEF) I want to change the font style to italic and the font color to black. How do I write these? And do I write it within my those functions in JavaScript or do style changes get applied directly within CSS or HTML?

These are the two functions I want to apply the style changes to:

    //this function changes the French phrase to an English phrase.
function swapFE(e) {
       var phrase = e.srcElement; 
       //phrase.innerText = english[phrase.id];
    var parent = phrase.parentNode;
    //childNodes[0] is the number of the phrase +1 
    var idnum = parent.childNodes[0];
    //parseInt takes a textstring and extracts it to make a number. Then you will subtract 1 from the number.
    var phrasenum = parseInt(idnum.innerHTML)-1;
    phrase.innerText = english[phrasenum];

  }


function swapEF(e) {
       var phrase = e.srcElement; 
       //phrase.innerText = english[phrase.id];
    var parent = phrase.parentNode;
    var idnum = parent.childNodes[0];
    var phrasenum = parseInt(idnum.innerHTML)-1;
    phrase.innerText = french[phrasenum];

If you could even just point me to a reference where I can find these properties that'd be great.

like image 664
Ashley Avatar asked Jan 31 '10 02:01

Ashley


People also ask

Can JavaScript add styles to Web page?

The HTML DOM allows JavaScript to change the style of HTML elements.

Can we style using JavaScript?

We can use JavaScript to directly set a style on an element, and we can also use JavaScript to add or remove class values on elements which will alter which style rules get applied.


1 Answers

obj.style.whicheverProperty = "value"

for instance:

document.getElementById('mydiv').style.fontVariant = "italic";

The google keyword you're looking for is HTML DOM (document object model), which defines the various styles as properties of an object's style member.

like image 140
Tor Valamo Avatar answered Sep 16 '22 18:09

Tor Valamo