Solved

Get first line of paragraph

Posted on 2009-04-01
6
427 Views
Last Modified: 2012-05-06
Can't quite figure this one out. I need to be able to select the text in the first line of a paragraph tag and wrap it in a <span> with an assigned class.

Thanks.
0
Comment
Question by:qwertq
6 Comments
 
LVL 13

Accepted Solution

by:
drypz earned 250 total points
ID: 24046088
Hi! Try this one.
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <style type="text/css">
 
        .spanClass
        {
            font-size:11px;
            font-family:verdana;
            color:Blue;
        }
    </style>
        
    <script type="text/javascript">
       
        function GetFirstParagraph()
        {
            var theText = document.getElementById('paragraph1').innerHTML;
            
            if (theText.length > 0)
            {
                var splittedText = theText.split('<BR>');
                CreateSpan(splittedText[0]);
            }
 
            return false;
        }
        
        function CreateSpan(textValue)
        {
            var theSpan = document.createElement("span");
            theSpan.id = "span1";
            theSpan.className = "spanClass";
            theSpan.innerText = textValue;
            document.body.appendChild(theSpan);
            
        }
 
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <p id="paragraph1">
            This is a test.<br />
            This is a test 1.<br />
            This is a test 2.<br />
        </p>
    </div>
    <div>
            <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return GetFirstParagraph();"/>
    </form>
</body>
</html>

Open in new window

0
 

Author Comment

by:qwertq
ID: 24049828
Thanks for the reply. Unfortunatly I can not use <br>'s to detect where the line break occurs. The text would be a normal paragraph of text inside a <p>, forced line breaks.

I am beginning to think this is not possible.
0
 
LVL 2

Expert Comment

by:arunrajesh
ID: 25717609
You can get the first line of a paragraph only if you know how many characters you are supporting for a line(It can be taken by identifying from your div width). Once you know the number of characters then use can split the content with that count using javascript to get the first line alone.
(I am not sure whether all browsers will support same number of characters - i guess it as "No")
just give a try.
0
 
LVL 14

Expert Comment

by:sam2912
ID: 27461400
This is not possible as different browsers display pages differently, and paragraph may wrap differently due to the width of the browser window, or the width of other elements next to it.
0
 
LVL 16

Expert Comment

by:CWS (haripriya)
ID: 37089679
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Web experts ! HTML ! Javascript project for medical MRI sharing 24 49
Add shadow behind div 5 24
VBScript on Html 15 47
binding event to form in window.open 9 35
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

790 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