SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Guru
    Join Date
    Mar 2006
    Posts
    701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    get text from div

    I want to get the count of characters of a div.
    Now I use
    Code:
    m= $('content_text').innerHTML;
    but is a way to clean html code and get only the number of characters of the pure text of that div?
    I use mootools.

  2. #2
    SitePoint Addict
    Join Date
    Dec 2008
    Location
    Brussels
    Posts
    377
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Guru
    Join Date
    Mar 2006
    Posts
    701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes,but also the text has html code so I want to clean it...
    Practically I want the height of the text at this div.
    If no other way,I will use length()...

  4. #4
    SitePoint Guru
    Join Date
    Mar 2006
    Posts
    701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So I tried that.Any suggestions are welcome.
    Code:
    function checkheght(mtext) {
            //alert(mtext);
            var div = document.createElement("div");
     div.innerHTML=mtext;
            // Test
            div.style.visibility = 'hidden';
          //  div.style.paddingLeft = '1px';
            document.body.appendChild(div);
            isSupported = div.clientHeight + 1;
     alert(isSupported)
            // Cleanup
            document.body.removeChild(div).style.display = 'none';
            div = null;
     
            return isSupported;
        }

  5. #5
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Never used mootools before... hope this works.

    I am doing a basic test against 2 divs, 1 with a P tag and getting the length of each
    PHP Code:
    <html>
        <
    head>
            <
    title></title>
            <
    script src="https://ajax.googleapis.com/ajax/libs/mootools/1.3.1/mootools-yui-compressed.js"></script>
        </head>
        <body>
            <div id="test">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>
            <div id="test2"><p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p></div>

            <script>
                (function() {
                    var test = document.id('test').get('text'),
                        test2 = document.id('test2').get('text');
                    alert('Test Div is ' + test.stripScripts().length + ', and Test2 Div is ' + test2.stripScripts().length);            
                }());
            </script>
        </body>
    </html> 

  6. #6
    SitePoint Guru
    Join Date
    Mar 2006
    Posts
    701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by centered effect View Post
    Never used mootools before... hope this works.

    I am doing a basic test against 2 divs, 1 with a P tag and getting the length of each
    PHP Code:
    <html>
        <
    head>
            <
    title></title>
            <
    script src="https://ajax.googleapis.com/ajax/libs/mootools/1.3.1/mootools-yui-compressed.js"></script>
        </head>
        <body>
            <div id="test">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>
            <div id="test2"><p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p></div>

            <script>
                (function() {
                    var test = document.id('test').get('text'),
                        test2 = document.id('test2').get('text');
                    alert('Test Div is ' + test.stripScripts().length + ', and Test2 Div is ' + test2.stripScripts().length);            
                }());
            </script>
        </body>
    </html> 
    Thank you.This code is helpful.
    But my I forgot to consider that I wanted to find the height of the text at that div,so I believe that the code I gave is closer to that I want because it check how it is formatting.


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
  •