473,396 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Displaying a bold text with a JavaScript

In HTML I use <b>...</b> to make parts of a text bold.
<h1 class = "Style-MyText">This text is normal. <b>This text is
bold.</b></h1>

Now I'd like to do the same with a JavaScript.
I tried
HTML: <h1 id = "MyID1" class = "Style-MyText">.</h1>
javascript: document.getElementById("MyID1").firstChild.replac eData(0,
document.getElementById("MyID1").firstChild.nodeVa lue.length, "This text is
normal. <b>This text is bold.</b>");

Unfortunately <b>...</b> is displayed as '<b>' and '</b>' and not
interpreted to make the text bold.
'View Selection Source' shows: This text is normal. &lt;/b&gt;This text is
bold.&lt;/b&gt;

Is there a way to make some parts of a text bold with a JavaScript?
Stefan
Nov 23 '05 #1
5 30056

Stefan Mueller wrote:
In HTML I use <b>...</b> to make parts of a text bold.
<h1 class = "Style-MyText">This text is normal. <b>This text is
bold.</b></h1>

Now I'd like to do the same with a JavaScript.
I tried
HTML: <h1 id = "MyID1" class = "Style-MyText">.</h1>
javascript: document.getElementById("MyID1").firstChild.replac eData(0,
document.getElementById("MyID1").firstChild.nodeVa lue.length, "This text is
normal. <b>This text is bold.</b>");

Unfortunately <b>...</b> is displayed as '<b>' and '</b>' and not
interpreted to make the text bold.
'View Selection Source' shows: This text is normal. &lt;/b&gt;This text is
bold.&lt;/b&gt;

Is there a way to make some parts of a text bold with a JavaScript?
Stefan


Try the following:
var elem = document.getElementById("MyID1");
var bold = document.createElement("b");

bold.appendChild(document.createTextNode("This text is bold."));
elem.appendChild(document.createTextNode("This text is normal."));
elem.appendChild(bold);

Another note: Since you are using the header tags, the text is already
bold in between, you can try using perhaps <div> tags to see a better
result.

Nov 23 '05 #2
You could put <span> or <div> tags around the text you want to alter
then use javascript to set the value of font-weight.

HTML:
<div id="text1">The text to change goes here</div>

javascript:
var t1 = document.getElementById("text1");
t1.style.fontWeight = 700;

You need to either put the JavaScript in a function or after the <div>
in the code otherwise it won't find the element. (fontWeight =400 is
normal text)

Nov 23 '05 #3
Many thanks for your replies.

I found in the Internet that <b> can also have an ID. Therefore I'm doing it
like
HTML: <h1 class = "Style-MyText">This text is normal. <b id = "MyID1"
class = "Style-MyText">.</b></h1>
javascript: document.getElementById("MyID1").firstChild.replac eData(0,
document.getElementById("MyID1").firstChild.nodeVa lue.length, "This text is
bold.");

Stefan
Nov 23 '05 #4
Stefan Mueller wrote on 15 nov 2005 in comp.lang.javascript:
Many thanks for your replies.

I found in the Internet that <b> can also have an ID. Therefore I'm
doing it like
HTML: <h1 class = "Style-MyText">This text is normal. <b id =
"MyID1"
class = "Style-MyText">.</b></h1>
javascript:
document.getElementById("MyID1").firstChild.replac eData(0,
document.getElementById("MyID1").firstChild.nodeVa lue.length, "This
text is bold.");

Normal
<br>
<b id='b'>Bold</b>
<br>

<button onclick=
"document.getElementById('b').style.fontWeight='20 0'"200</button> <button onclick=
"document.getElementById('b').style.fontWeight='40 0'"400</button> <button onclick=
"document.getElementById('b').style.fontWeight='60 0'"600</button> <button onclick=
"document.getElementById('b').style.fontWeight='80 0'"800</button>


--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Nov 23 '05 #5
> Normal
<br>
<b id='b'>Bold</b>
<br>

<button onclick=
"document.getElementById('b').style.fontWeight='20 0'"
200</button>


Wow, that's cool.

Many thanks
Stefan

Nov 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: JimJones | last post by:
Hey, I don't really know much about ASP but I need some help modifying the way a field is currently displayed. Currently the ASP code inside my page is simply: <%=rs.Fields("Notes")%> ...
0
by: Engineerik | last post by:
I have looked at the syncfusion datagridformattabletextboccolumn sample but I have been unable to figure out how to base the special formatting on whether the datasource for the cell has a changed...
2
by: Kejpa | last post by:
Hi, I'm experiencing problems with showing bold text in a treeview. The text gets cut and the last character isn't fully visible. It seems like the width of the text is based on the non-bold size...
2
by: genojoe | last post by:
Is it possible to include one line of bold text followed by one line of regular text in the MsgBox control? What I would like to see is the following lines <Bold>The text you entered isn't an...
1
by: Tor Inge Rislaa | last post by:
Finding Bold Text (or other formatting attributes) in RichTextbox . Is it possible to find and select bold text by code within the text of a RichTextbox? What I want is to manipulate the text...
9
by: Erwin Moller | last post by:
Hi, Can anybody comment on this? In comp.lang.php I advised somebody to skip using: <script language="javascript"> and use: <script type="text/javascript"> And mr. Dunlop gave this response:
1
by: anatak | last post by:
Hello I have function that generates a list op options for a select dropdown box here is the function I am pretty sure that the problem is situated in the new Option() function function...
11
by: pardesiya | last post by:
Friends, I am having trouble displaying Japanese text within a textbox (or anywhere else) in an aspx page with .net 2.0 framework. Initial default text in Japanese displays perfectly but when I...
1
by: runway27 | last post by:
how to convert text into bold using javascript and use this bold text in CONFIRM box
0
by: Guilherme Polo | last post by:
On Thu, Sep 18, 2008 at 1:06 PM, April Lekin <lekin2@llnl.govwrote: Yes. You could separate them in two labels (if you are using labels), or apply a different tag if you are using a Text...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.