You are here: Reference > JavaScript > client-side > event handling > properties > data (event)

data property (event)

Browser support:
839.5
Returns the characters entered in case of the textInput event or the contents of the message for the onmessage event.
Note: The data property is supported in Internet Explorer from version 8, in Firefox from version 3 and in Opera from version 9.5.
For further details, see the pages for the textInput and onmessage events.

Syntax:

object.data;
You can find the related objects in the Supported by objects section below.
This property is read-only.

Possible values:

String that retrieves the entered characters or the contents of the message.
Default: this property has no default value.

Example HTML code 1:

This example illustrates the use of the data property for textInput events:
<head>
    <script type="text/javascript">
        function Init () {
            var textarea = document.getElementById ("textarea");
            if (textarea.addEventListener) {    // all browsers except IE before version 9
                    // Google Chrome and  Safari
                textarea.addEventListener ("textInput", OnTextInput, false);
                    // Internet Explorer from version 9
                textarea.addEventListener ("textinput", OnTextInput, false);
            }
        }

        function OnTextInput (event) {
            alert ("The following text has been entered: " + event.data);
        }       
    </script>
</head>
<body onload="Init ();">
    Enter or paste some characters into the textarea!
    <textarea id="textarea">Textarea</textarea>
</body>
Did you find this example helpful? yes no

Example HTML code 2:

This example illustrates the use of the data property for onmessage events:
Code
message.htm
<head>
    <script type="text/javascript">
        function Init () {
            if (window.addEventListener) {  // all browsers except IE before version 9
                window.addEventListener ("message", OnMessage, false);
            }
            else {
                if (window.attachEvent) {   // IE before version 9
                    window.attachEvent("onmessage", OnMessage);
                }
            }
        }

        function GetState () {
            var frame = document.getElementById ("myFrame");
                // send the 'getstate' message to the frame window
            var message = "getstate";
            if (frame.contentWindow.postMessage) {
                frame.contentWindow.postMessage (message, "*");
            }
            else {
                alert ("Your browser does not support the postMessage method!");
            }
        }

        function OnMessage (event) {
            var message = event.data;
            var arr = message.split (",");
            if (arr[0] == "true") {
                alert ("The check box is checked.");
            }
            else {
                alert ("The check box is not checked.");
            }

            var selIndex = Number (arr[1]);
            alert ("The " + (selIndex + 1) + ". option is selected.");
        }
    </script>
</head>
<body onload="Init ();">
    <iframe id="myFrame" src="message.htm" width="500" height="200px"></iframe>
    <br /><br />
    <button onclick="GetState ()">Get the state of controls in the frame</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content