SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2002
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Formatting Lines in a Variable

    Hey everyone.

    I'm trying to figure out how to do this in javascript. Here's the setup:

    There is a variable:
    input = window.document.sidegenerate.input.value;
    This variable points to a textarea of a form. Meaning that it has multiple lines.

    What I want to do is have code added before and after each line. Example:

    Original Input:
    Line 1
    Line 2
    Line 3
    Line 4
    etc

    This would be the output:
    <h2>Line 1</h2>
    <b>Line 2</b>
    <b>Line 3</b>
    <b>Line 4</b>
    <b>etc</b>

    As you can see, all the lines have had the same formatting added to them except for the first line. Which has different formatting.

    So is this possible in javascript? Or have I bitten off more than I can chew? If someone could shed some light on this problem I would appreciate it.

    Thanks.
    Peter

  2. #2
    SitePoint Zealot
    Join Date
    Jul 2002
    Location
    Fort Lauderdale
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I made you a present!
    http://www.unpossible.cc/string_functions.html
    Since you are formatting it for HTML, you might want to replace all line breaks(\r) with <br>

    www.unpossiblemedia.com - A Fort Lauderdale based multimedia company.

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2002
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Superb!

    One request though, is it possible to have the <B> and </B> tags on every line? I plan on using this for a more complicated script, and I just used the <b> and <h2> tags to demonstrate what I was going for.

    Other than that, great job!

    Thanks Again,
    Peter

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2002
    Location
    Fort Lauderdale
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    www.unpossiblemedia.com - A Fort Lauderdale based multimedia company.

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2002
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Again. Sorry to be such a pain, but is it possible to have the <b>line #</b> on the same line? There seems to be a line break after the <b>

    Also, do you know why it doesn't work in Mozilla or Opera?

    Thanks,
    Peter

  6. #6
    SitePoint Zealot
    Join Date
    Jul 2002
    Location
    Fort Lauderdale
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by peternm22
    is it possible to have the <b>line #</b> on the same line?
    Yes, just take out the \r after </b>. Not sure why it doesn't work in other browsers, you could play with the code yourself you know

    www.unpossiblemedia.com - A Fort Lauderdale based multimedia company.

  7. #7
    SitePoint Enthusiast
    Join Date
    Oct 2002
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Vasudevelopa
    Yes, just take out the \r after </b>. Not sure why it doesn't work in other browsers, you could play with the code yourself you know

    I have been playing with the code, the problem is that most of it is WAY over my head.

    If I take the \r after the </b> out, then it formats it like this:

    <h2>line1</h2>
    <b>
    line2</b><b>
    line3</b><b>
    line4</b>

    Am I missing something?

    Thanks,
    Peter


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
  •