473,973 Members | 13,524 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to enclose <p> tags with <strong> tags?

4 New Member
:confused:

I've asked my professor about this one a couple of times in email and she gave me some tips but I still can't nail this one down. Our book only gives one example of using .innerHTML and it's not very relevant to this problem so I want to ask you JS proz out there.

We're supposed to loop through the document.all collection and find all the <p> tags and enclose them in <strong> tags. I'm pretty sure I've got most of the script right except for the crucial line where you add the <strong> tags. I've tried a lot of variations but I just don't understand enough about how .innerHTML works to get this right and I can't seem to find it on the Web.

Thanks for any help you can provide. Here's the code...

Expand|Select|Wrap|Line Numbers
  1. <script type = "text/javascript">
  2.    <!--
  3.  
  4.       function go() {
  5.  
  6.          for( var x = 0; x < document.all.length; ++x ) {
  7.  
  8.             if( document.all[ x ].tagName == "p" ) {
  9.  
  10.                document.all[ x ].innerHTML = "<strong>" + 
  11.                   document.all[ x ].innerHTML + "</strong>";
  12.             }
  13.  
  14.          } //end for
  15.       } //end go
  16.  
  17.  // -->
  18.    </script>
  19.  
  20. </head>
  21.  
  22. <body onload = "go()">
  23.  
  24.    <p>
  25.  
  26.       This is a sentence that is enclosed in p-tags.  It is going to be
  27.       enclosed in strong tags as well once the javascript gets through
  28.       with it.  :o)
  29.  
  30.    </p>
  31.  
  32.    <p>
  33.  
  34.       For I am not ashamed of the gospel, for it is the power of God for
  35.       the salvation of all who believe, first for the Jew and then for
  36.       the Gentile.
  37.  
  38.    </p>
  39.  
  40.    <p>
  41.  
  42.       When I'm done with this, I'm going to bed.  I'm tired.  :o)  I watched
  43.       Good Night and Good Luck with my wife tonight.  It was interesting.
  44.       I learned a lot about the clash of CBS & Ed Murrow and McCarthy the
  45.       Communist head hunter.
  46.  
  47.    </p>
  48.  
  49.    <p style = "font-weight: bold">Here's an example of what bold text should
  50.       look like.</p>
  51.  
  52.  
  53. </body>
  54. </html>
Jul 13 '06 #1
3 2058
Banfa
9,065 Recognized Expert Moderator Expert
document.all is not a part of the HTML/Javascript DOM it is IE specific and there for wont work on Firefox which is what I use. You can get all the paragraphs using the function document.getEle mentsByTagName( ...), when I changed you code to do this it worked.

Expand|Select|Wrap|Line Numbers
  1. <script type = "text/javascript">
  2.   <!--
  3.  
  4.          function go() {
  5.  
  6.     var pars = document.getElementsByTagName("p");
  7.         for( var x = 0; x < pars.length; ++x ) {
  8.  
  9.               pars[ x ].innerHTML = "<strong>" + 
  10.                       pars[ x ].innerHTML + "</strong>";
  11.  
  12.         } //end for
  13.      } //end go
  14.  
  15.  // -->
  16.   </script>
  17.  
Jul 13 '06 #2
Swaine77
4 New Member
Thanks a lot for the tip. I'd still like to know how to use IE's document.all better.
Jul 14 '06 #3
Arielle
76 New Member
You need to use an uppercase P in your test. It is case-sensitive and it's looking for uppercase.

if( document.all[ x ].tagName == "P" ) {


I ran it with this change and your code works just fine. I've made this mistake a million times too... strange how capitalization and syntax make for so many minor mistakes to hurt our brains :)
Jul 14 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

129
64674
by: Torbjørn Pettersen | last post by:
I've started cleaning up my HTML and implementing CSS. So far I've used FrontPage, but am switching over to DreamWeaver. Reading a bit on W3Schools.com and W3.org I see there are a lot of HTML code I never even knew existed. Among these are <em> and <strong>. What's the difference between these two and <i> and <b>? -- Torbjørn Pettersen Editor/Webmaster
0
10347
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
10160
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11810
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
11560
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
7600
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
6409
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...
1
5146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4726
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3755
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.