Friday, 5 February 2016

Copy text using javascript

<div style="position:absolute;">
        <hr />
        Test by dharmendra
        <script>

        function copyfieldvalue(e, id) {
            var field = document.getElementById(id)
            field.focus()
            field.setSelectionRange(0, field.value.length)
           // window.prompt("Copy to clipboard: Ctrl+C, Enter", field.value);
            var copysuccess = copySelectionText()
            if (copysuccess) {
                showtooltip(e)
            }
        }
        function copySelectionText() {
            var copysuccess // var to check whether execCommand successfully executed
            try {
                copysuccess = document.execCommand("copy") // run command to copy selected text to clipboard
            } catch (e) {
                copysuccess = false
            }
            return copysuccess
        }
        </script>

        <fieldset style="max-width: 600px">
            <legend>Share this tutorial</legend>
            <input id="url" type="text" size="60" value="http://www.javascriptkit.com/javatutors/copytoclipboard.shtml" />
            <a href="#" onclick="copyfieldvalue(event, 'url');return false">Copy</a>
        </fieldset>

No comments:

Post a Comment