473,383 Members | 1,737 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,383 software developers and data experts.

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

: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 2033
Banfa
9,065 Expert Mod 8TB
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.getElementsByTagName(...), 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
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
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
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.