Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

JavaScript DOM inquiry

Posted on 2007-10-11
4
Medium Priority
?
528 Views
Last Modified: 2013-11-19
Hey all,

I'm just trying to learn some basic DOM manipulation with core javascript. I know how simple it is to do something like this using a library like jQuery or prototype, but for now my interests remain in obtaining results similar to these without them.  In this example, I'm looking over a book and noted how they're uppercasing all letters.  However, I can't get it to work. What kind of node should I be passing to get Node.TEXT_NODE for this to work?

here's my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html lang="en">
<head>
      <script type="text/javascript">
      // This function recursively looks at Node n and its descendants,
      // converting all Text node data to uppercase
      function upcase(n) {
          if (n.nodeType == 3 /*Node.TEXT_NODE*/) {
              // If the node is a Text node, change its text to uppercase.
              n.data = n.data.toUpperCase();
          }
          else {
              // If the node is not a Text node, loop through its children
              // and recursively call this function on each child.
              var kids = n.childNodes;
              for(var i = 0; i < kids.length; i++) upcase(kids[i]);
          }
      }
      
      </script>
</head>
<body>
<p id="test">
      This is a test
</p>
<ul>
      <li>This
      <li>Is
      <li>Also
      <li>a
      <li>test
      <br>
      <button onclick="upcase('test')">UpperCase it up!</button>
</body>
</html>
0
Comment
Question by:stevefNYC
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
basicinstinct earned 2000 total points
ID: 20062680
call it like this:

<button onclick="upcase(document.body)">UpperCase it up!</button>
0
 
LVL 3

Author Comment

by:stevefNYC
ID: 20062693
Dang, you know how I tested it at one point. I used:

<button onclick="upcase('document')">UpperCase it up!</button>

Had the right idea, but passing it as a string doesn't work. :-(

Thank you though for the help.
0
 
LVL 14

Expert Comment

by:steveberzins
ID: 20062698
I think you just need to actually get the node, not pass in the string 'test' to your function call.

      <button onclick="upcase(document.getElementById('test'))">UpperCase it up!</button>
0
 
LVL 23

Expert Comment

by:basicinstinct
ID: 20062699
no problem - at least you'll never forget it now...
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
This article discusses how to implement server side field validation and display customized error messages to the client.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question