-4

i know how to set font style italic in HTML but now i'm trying to do that only with java script . Is it possible to do that in java script ? . Can someone help\clarify me pls . Here is my code ,

 myJson.name.push(mainSteps.name); // Need to changes this in italic (js code)
Saravana Kumar
  • 179
  • 2
  • 5
  • 18

7 Answers7

4

A JavaScript string itself doesn't have a concept of a "style". Styling only applies when you output the string somewhere. If you output it to HTML, you can use HTML or CSS to style it.

So if you are asking whether there is an "output-agnostic" way to style a JavaScript string, the answer is no.


Btw, the code you wrote is JavaScript (assuming you pass a proper value for the ID):

document.getElementById(#Html id).style.fontStyle = "italic";

and if you want to style the HTML output, then this would be the way to go.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • I tend to disagree, just for the sake of completeness. JavaScript has some functions for let say html string manipulation like in this case [`italics`](https://tc39.github.io/ecma262/#sec-string.prototype.italics). They are "deprecated", but for now they are not likely to disappear. [ECMA spec Annex B](https://tc39.github.io/ecma262/#sec-additional-ecmascript-features-for-web-browsers) the note. Same reason why Win 9 is skipped. And here is the [Fiddle](https://jsfiddle.net/ahtt9p6s/). – Bakudan Aug 16 '16 at 12:45
  • @Bakudan: I knew someone would bring this up ;) Since you already link to the section, it clearly says: *"These features are not considered part of the core ECMAScript language. Programmers should not use or assume the existence of these features and behaviours when writing new ECMAScript code."*. But even if you ignore that, performing HTML manipulation is not the same of having a concept of "style". – Felix Kling Aug 16 '16 at 13:19
  • Fair enough :) Personally I hope everybody will just forget about these methods :D – Felix Kling Aug 16 '16 at 13:56
2

In Javascript you can not change font style because it does not exist in javascript as such. Font style matters only when the data has to be shown which is when it is written on some where in HTML .This is how you may change the fontstyle for entire body

    This is in italics
    <script>
    function italicsBody() {
        document.body.style.fontStyle = "italic";
    }
    italicsBody()
    </script>

Similary if you want the same for specifi data you may do it using getElemenById . Check the following code

<ul id="names">
    </ul>
    
    <script>
    function italicsBody() {
        document.getElementById("names").style.fontStyle = "italic";
    }
    function populateNames() {
      var names = ["name1","name","name3"]
      var nameList = document.getElementById("names");
      for (var key in names) {
          nameList.innerHTML = nameList.innerHTML + ' <li> ' + names[key] + '</li>';
      }
    }
    populateNames()
    italicsBody()
    
    </script>

Here is the jsfiddle for the same jsfiddle

1
document.getElementById("myP").style.fontStyle = "italic";
Vijayabaskaran M
  • 613
  • 6
  • 12
1

You can use "somestring".italics() to get "<i>somestring</i>"which might be what you are after. I suggest you use CSS as others said though - italics is not standard anymore.

Spikolynn
  • 4,067
  • 2
  • 37
  • 44
0

I Given One Example As Below

function myFunction() {
    document.getElementById("myP").style.font = "italic 15px arial,serif";
}
myFunction();
<p id="myP">This is a paragraph.</p>
Samudrala Ramu
  • 2,088
  • 1
  • 15
  • 36
0

This Is Second Example When User Click On Button Then Style Applying From Java script .

function myFunction() {
    document.getElementById("myP").style.font = "italic 20px arial,serif";
}
<p id="myP">This is a paragraph.</p>

<button type="button" onclick="myFunction()">Set font</button>
Samudrala Ramu
  • 2,088
  • 1
  • 15
  • 36
-2

myJson.name.push(mainSteps.name).style.fontStyle = "italic";

It is done the same way.

Blake Connally
  • 689
  • 1
  • 4
  • 16