Monday, October 15, 2012

jQuery html() Method

How to set and get inner HTML using jQuery

In jQuery we can get inner HTML using html(0 method, This method will return inner HTML only first element in document.

i.e if jQuery found more than one selector then its consider only first selector.

Syntax -
1. For get use .html() - To get inner HTML of selector use this method.
2. For set use - .html(string) - To set inner HTML, with one string parameter, this parameter consider as HTML.

Example 1 - You can see following simple example to see how html() funciton work.

<!DOCTYPE html>
<html>
<head>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script>
        function setText() {
            $('message').html('This is testing message');
        }

        function getText() {
            var text = $('message').html();
            alert(text);
        }
    </script>
</head>
<body>
    <div id="message">This is demo message</div>
    <input type="button" onclick="setText();"/>
    <input type="button" onclick="getText();"/>
</body>
</html>
 

No comments:

Post a Comment

Total Pageviews