SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question add spans to all p tags

    perhaps an odd request but i need ALL (not just one id) p elements to have spans.

    so on document load...
    HTML Code:
    <p>text</p>
    would become...
    HTML Code:
    <p><span>text</span></p>
    any help would be incredibly appreciated!

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Load the script from the end of the body

    Code html4strict:
    <html>
    <head>
    ...
    </head>
    <body>
    ...
    <script type="text/javascript" src="js/script.js"></script>
    </body>
    </html>

    where the script retrieves all p elements, moves their contents to a span, and then moves the span back into the p element

    js/script.js
    Code javascript:
    var els = document.getElementsByTagName('p'),
        p,
        span,
        i;
    for (i = 0; i < els.length; i += 1) {
        p = els[i];
        span = document.createElement('span');
        while (p.hasChildNodes()) {
            span.appendChild(p.firstChild);
        }
        p.appendChild(span);
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why do that with JavaScript? Sounds like a DOM-related homework question to me.
    Tab-indentation is a crime against humanity.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •