Italicizing Java text box

668 Views Asked by At

I need to update a code by having the name appear in italics in the following sentences.
Where in the innerHTML would I place the tags?

`enter code here`
<html>
 <head>
  <title>
   Greetings
  </title>
  <meta charset="us-ascii">
 </head>
 <body>
  <h2>Greetings</h2>
  <p>
   Enter your name:
   <input type="text" id="nameBox" size=12 value="">
  </p>
  <input type="button" value="Click for Greeting"
   onclick="
    document.getElementById('outputDiv').innerHTML=
     'Hello '
     + document.getElementById('nameBox').value
     + ', welcome to my page.<br>Do you mind if I call you '
     + document.getElementById('nameBox').value
     + '?';
    ">
   <hr>
   <div id="outputDiv">
  </div>
 </body>
</html>
2

There are 2 best solutions below

6
On

I tested the following code, it should make the reply italicized. FYI all changes to the look of the output can be done using CSS (the style tags). Also, it is a good idea to keep HTML, JavaScript, and CSS components isolated from one another.

<!DOCTYPE html>
<html>
     <head>
        <title> Greetings</title>
        <meta charset="us-ascii">
        <script type="text/javascript">
            function greeting(){
                document.getElementById('outputDiv').innerHTML=
                'Hello <span class="name">'
                + document.getElementById('nameBox').value
                + '</span>, welcome to my page.<br>Do you mind if I call you <span class="name">'
                + document.getElementById('nameBox').value
                + '</span>?';
           }
        </script>
         <style type="text/css">
            span.name{
                font-style:italic;
            }
         </style>
     </head>
     <body>
         <h2>Greetings</h2>
         <p>
            Enter your name:<input type="text" id="nameBox" size=12 value="">
         </p>  
         <input type="button" value="Click for Greeting" onclick="greeting()">
          <hr>
          <div id="outputDiv">
         </div>
     </body>
</html>
0
On

The tag for italics is <i>. You can wrap it around an element to make it appear in italics. your name is <i> unknown to us </i>

Don't use this tag!


This is acceptable for (1) applications where you need to save each byte, or (2) situations where you need to support ancient browsers like Netscape 1.0.
Even email clients support the <em> tag or embedded CSS.

A better way is to use the <em> tag.
It means "emphasize". Most browsers display such text in italics. The difference is with browsers that can't render italics. When such a browser encounters <i> it skips the tag and renders its contents without effect. When it encounters <em> it emphasizes the text by any means available. Using bold instead, changing the text color, inserting spaces... . For a screen reader, it could mean "slow down by 10% and increase the volume by 5%".

The modern way is to tag by semantics, and style by CSS:

your name is <span class="name"> unknown to us </span>

and in a separate CSS file

span.name { font-style: italic; }