","upvoteCount":12},{"@type":"Answer","text":"You can use the tag!\n\ndocument.write('the result of multiplying the two numbers is ' + result + '');\n\nlove , I edited my answer.","upvoteCount":2},{"@type":"Answer","text":"love, go with the tag again, define the color in style attribute, place number1 in b/w the and you're done!","upvoteCount":1},{"@type":"Answer","text":"Thank you so much for the help. God bless you.","upvoteCount":1},{"@type":"Answer","text":"Thank you it works. How about if I wanted to change the color of the var number1 and number2\ndocument.write(number1)","upvoteCount":0},{"@type":"Answer","text":"One last question:\nCan I define the variable tag for the two numbers with the span at the beginning\nBecause in the assignment I have to repeat the numbers in document.write\n","upvoteCount":0},{"@type":"Answer","text":"Of course, you have to use some CSS:\n\nspan.result {\n /* for result variable */\n color: #00F;\n}\nspan.variable {\n /* for variable */\n color: #0F0;\n}\n\nSo when you write result to document, include\n\"\" + result + \"\"\nSimilarly, for the variable!\n\nHope it helps!","upvoteCount":0},{"@type":"Answer","text":"Thanks. It does.","upvoteCount":0},{"@type":"Answer","text":"Blue, how about the font? How do I define it within the span tag?","upvoteCount":0},{"@type":"Answer","text":"Hi Jaydeep Khatri,\n\na.style.fontSize = \"50\" will do nothing to the paragraph, the reason is that we have many ways to define the font size i.e., px, pt, %, vh and vw.\nSo, to make the font size upto 50px you should use \"50px\" as the value of the fontSize property of style attribute. \nThank you!","upvoteCount":0}]}
}
+ 1
Font color and size
How do I change the font color and size of only one element in JavaScript
For example:
var number1=5
var number2=7
var result=number1*number2
document.write("the result of multiplying the two numbers is" + result)
Question: how do I change the color of only the result.
Thanks this is for a school assignment.
<p id="test"> Hello World! </p>
<script>
var a = document.getElementById('test');
a.style.fontSize = "50px"; //50 pixels
a.style.color = "#f00"; //yellow color
</script>
2nd Apr 2018, 4:51 PM
Jaydeep Khatri
+ 2
You can use the <span> tag!
document.write('the result of multiplying the two numbers is <span style="color: #00F; font: bolder 12pt arial;">' + result + '</span>');
love , I edited my answer.
2nd Apr 2018, 3:27 PM
777
+ 1
love, go with the <span> tag again, define the color in style attribute, place number1 in b/w the <span> and you're done!
2nd Apr 2018, 3:57 PM
777
+ 1
Thank you so much for the help. God bless you.
2nd Apr 2018, 3:59 PM
love
0
Thank you it works. How about if I wanted to change the color of the var number1 and number2
document.write(number1)
2nd Apr 2018, 3:54 PM
love
0
One last question:
Can I define the variable tag for the two numbers with the span at the beginning
Because in the assignment I have to repeat the numbers in document.write
2nd Apr 2018, 4:04 PM
love
0
Of course, you have to use some CSS:
span.result {
/* for result variable */
color: #00F;
}
span.variable {
/* for variable */
color: #0F0;
}
So when you write result to document, include
"<span class='result'>" + result + "</span>"
Similarly, for the variable!
Hope it helps!
2nd Apr 2018, 4:11 PM
777
0
Thanks. It does.
2nd Apr 2018, 4:20 PM
love
0
Blue, how about the font? How do I define it within the span tag?
2nd Apr 2018, 10:06 PM
love
0
Hi Jaydeep Khatri,
a.style.fontSize = "50" will do nothing to the paragraph, the reason is that we have many ways to define the font size i.e., px, pt, %, vh and vw.
So, to make the font size upto 50px you should use "50px" as the value of the fontSize property of style attribute.
Thank you!