iframe set value on input outside js code example

Example 1: iframe set value on input outside js

<body>
    <input id="input-field" />
</body>

Example 2: iframe set value on input outside js

<body>
    <iframe id="site2-frame" src="http://www.mysite2.com/index.php"></iframe>
</body>

Example 3: iframe set value on input outside js

var input = document.getElementById('input-field');

window.addEventListener('message', function(e) {
    // Check the origin, accept messages only if they are from YOUR site!
    if (/^http\:\/\/www\.mysite1\.com/.test(e.origin)) {
        input.value = e.data; 
        // This will be 'Something something something'
    }
});

Example 4: iframe set value on input outside js

var frame = document.getElementById('site2-frame');

frame.contentWindow.postMessage('Something something something', '*');

Tags:

Misc Example