Friday 20 January 2012

JQuery Contents:-

<html>
<head>

</head>
<body>
       <div id='divcontents'>This <span>is</span><b> bold</b></div>
       
<script type="text/javascript" language="javascript">

        //contents function data
        var con = "";

        //contents function iterates through html elements and text nodes
        $('#divcontents').contents().filter(function () {
            //html element
            if (this.nodeType == 1) {
                //get the innerHtml
                con += $(this).html();
            }
            //text node
            if (this.nodeType == 3) {
                //get the text
                con += this.wholeText;
            }
        });

        //alert data
        alert(con); //displays This is bold

        //children function data
        var chi = "";

        //contents function iterates through html elements only
        $('#divcontents').children().filter(function () {
            //html element
            if (this.nodeType == 1) {
                //get the innerHtml
                chi += $(this).html();
            }
            //text node
            if (this.nodeType == 3) {
                //get the text
                chi += this.wholeText;
            }
        });

        //alert data
        alert(chi); //displays  is bold
    </script>
       
</body>
</head>



Let me know, if you have any feedback. Mail me for source code. Enjoy reading my articles…
sekhartechblog@gmail.com

No comments:

Post a Comment