471,873 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,873 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 1977
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

Post your reply

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

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.