Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • html vs text in jquery

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 289
    Comment on it

    Hello Readers,

    Below is the difference between html() and text() methods in jquery:

    1. Both .html() and .text() methods are used in jquery and both are jquery methods.

    2. .html() is faster as compared to .text() method in jquery.

    3. .html() method is only for html documents while .text() is used for both HTML as well as XML documents.

    4. .text() method is most simple as compared to .html() method.

    5. .html() method get the HTML content of the first element in the matched set while .text() method return all the string which display on browser or return the combines txt contents of all matched elements.

    6. .text() method cannot be used on input elements.

    7. For Example :

    html() :-

    <div id="testDiv">
            <p>Cricket</p>
            <p>Hockey</p>
            <p>Football</p>
    </div>
    <input type="button" id="firstButton" name="submitButton" value="Submit"/>
    <script type="text/javascript">
        $(document).ready(function(){
            $("#firstButton").click(function(){
                var content=$("#testDiv").html();
                alert(content);
            });
        });
    </script>
    
    <b>Output :</b>
    
    <p>Cricket</p>
    <p>Hockey</p>
    <p>Football</p>
    

    In the above code, it return the inner content of the div id textDiv inside a speific html element.

    text():-

    <div id="testDiv">
        <p>Cricket</p>
        <p>Hockey</p>
        <p>Football</p>
    </div>
    <input type="button" id="firstButton" name="submitButton" value="Submit"/>
    <script type="text/javascript">
        $(document).ready(function(){
            $("#firstButton").click(function(){
                var content=$("#testDiv").text();
                alert(content);
            });
        });
    </script>
    
    <b>Output :</b>
    
    Cricket
    Hockey
    Football
    

    In the above code, it gives the content of the div id textDiv and it can be used in XML as welll as HTML documents.

    It will return the string inside the the html elements.

    1. Another Example of both the methods when pass the html documents inside the methods:

    html():-

    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
        $("button").click(function(){
            $("p").html("<p>Hello world</p>");
        });
    });
    </script>
    </head>
    <body>
    <button>Set text content for all p elements</button>
    </body>
    </html>
    

    Output:

    Hello world

    text() :-

    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
        $("button").click(function(){
            $("p").text("<p>Hello world</p>");
        });
    });
    </script>
    </head>
    <body>
    <button>Set text content for all p elements</button>
    </body>
    </html>
    

    Output :

    Hello world

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: