473,652 Members | 2,979 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.getEle mentById("MyID1 ").firstChild.r eplaceData(0,
document.getEle mentById("MyID1 ").firstChild.n odeValue.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 30082

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.getEle mentById("MyID1 ").firstChild.r eplaceData(0,
document.getEle mentById("MyID1 ").firstChild.n odeValue.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.getEle mentById("MyID1 ");
var bold = document.create Element("b");

bold.appendChil d(document.crea teTextNode("Thi s text is bold."));
elem.appendChil d(document.crea teTextNode("Thi s text is normal."));
elem.appendChil d(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.getEle mentById("text1 ");
t1.style.fontWe ight = 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.getEle mentById("MyID1 ").firstChild.r eplaceData(0,
document.getEle mentById("MyID1 ").firstChild.n odeValue.length , "This text is
bold.");

Stefan
Nov 23 '05 #4
Stefan Mueller wrote on 15 nov 2005 in comp.lang.javas cript:
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.getEle mentById("MyID1 ").firstChild.r eplaceData(0,
document.getEle mentById("MyID1 ").firstChild.n odeValue.length , "This
text is bold.");

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

<button onclick=
"document.getEl ementById('b'). style.fontWeigh t='200'"200</button> <button onclick=
"document.getEl ementById('b'). style.fontWeigh t='400'"400</button> <button onclick=
"document.getEl ementById('b'). style.fontWeigh t='600'"600</button> <button onclick=
"document.getEl ementById('b'). style.fontWeigh t='800'"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.getEl ementById('b'). style.fontWeigh t='200'"
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
1893
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")%> This is fine for displaying the text, but the text contains links which are
0
1264
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 value. Ideally I would like to bold text for a cell if the datarowstate of the underlying datarow is anything other than unchanged. I know there must be a way but I haven't been able to do it. Anybody know how to do this?
2
2169
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 of the text. Does anyone recognize this behaviour? I'm using .NET 1.0, is it fixed in later versions? TIA Kejpa
2
3549
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 item in the list.<Bold> <Regular>Select an item from the list, or enter text that maches one of the listed items.<Regular>
1
3120
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 into HTML code. Replace the Bold text with <b>This is the bold text</bbefore I save it to an HTML file.
9
8408
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
1672
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 showKens(){ var kenList = document.form1.ken; var cityList = document.form1.city; var kenId = kenList.options.value; var cityName = '';
11
3764
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 attempt to change the text following a button-click event, it displays as junk. I have tried setting the globalization tag in the web.config file but that does not help eiter. <globalization requestEncoding="UTF-8" responseEncoding="UTF-8"
1
4394
by: runway27 | last post by:
how to convert text into bold using javascript and use this bold text in CONFIRM box
0
1593
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 widget. Changing the color is easier, you only change the foreground option.
0
8367
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8811
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8467
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8589
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7302
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5619
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4145
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1591
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.