In jQuery focus
by default scroll the page to the element. If we use focus({preventScroll: true})
our copy function will not work as you mension.
So I have use some css hack. See the following jQuery code.
function copyToClipboard(selector, event){
event.preventDefault();
var $temp = jQuery("<div>");
jQuery("body").append($temp);
$temp.attr("contenteditable", true)
.html(jQuery(selector).html()).css('position', 'fixed').select()
.on("focus", function() { document.execCommand('selectAll',false,null); })
.focus().css('position', 'static');
document.execCommand("copy");
$temp.remove();
}
This is working fine for me.
If you don’t want to use this hack you can also use some pure javascript code. For example this
Related Posts:
- jQuery equivalent of JavaScript’s addEventListener method
- What is content-type and datatype in an AJAX request?
- Checking a Url in Jquery/Javascript
- When and why to ‘return false’ in JavaScript?
- How can I display a tooltip message on hover using jQuery?
- How is JavaScript .on() method defined?
- correct way of comparing string jquery operator =
- JQuery: $.get is not a function
- Bootstrap carousel not sliding
- Jquery to change form action
- How can I expand and collapse a using javascript?
- Detecting value change of input[type=text] in jQuery
- Javascript : array.length returns undefined
- jQuery – checkbox enable/disable
- How can I correctly format currency using jquery?
- How do I send this form's output to a new WordPress page?
- Annoying “JQMIGRATE: Migrate is…” in console after update to WordPress 4.5
- Update jquery version
- Correct Method to run scripts with dependencies without enqueue?
- Can’t get a JSON object in response to an Ajax request with wp_ajax
- Using the WordPress Media Library and returning the Attachment ID rather than URL?
- WP set auth cookie using Ajax is not saved to browser
- Modify Custom Field in Media Library using JavaScript
- Loading jQuery With Two Fallbacks
- ajax call in wordpress front end
- Including jQuery in WordPress
- Comment form vaildation
- Enqueuing jQuery in plug-ins
- Script doesn’t load because of a javascript MIME type error
- TinyMCE – TypeError: r is undefined
- Auto-detect javascript errors and warn theme user
- combining js scripts from a js newb [closed]
- Heartbeat API: How to access data already enqueued?
- Combining media queries with image sizes
- Implementing lazy loading in homepage posts instead of pagination
- Jquery integration with my theme
- Cannot prefill hidden Ninja Forms fields
- How to show jquery popup only in one page or once?
- Jquery not showing in homepage
- jQuery Ajax returning correct Response, but after redirect to 404 Error
- Disable stored tags queries in admin page when posting a new post
- WordPress Comments – Rich Text
- Limit filetype and wp media
- Blip Slideshow plugin Mootools/jQuery conflict
- Register jQuery – OOP WP
- How to isolate code to the post edit screen
- How to tell WordPress that jQuery exists on the page to avoid duplicates via wp_enqueue_scripts
- Which jQuery packages are included in WordPress 3.1.2?
- jQuery code not working in widgets page
- Load random posts without refreshing page (jQuery)?
- How to use jquery included with wordpress for jpanelmenu
- Cannot get Jquery to work with wordpress
- WordPress error when replacing local jQuery by externally-hosted
- Create special WordPress slideshow with “jquery.cycle.all.js”
- Superfish Menu Not Loading
- datepicker for custom post type admin
- Convert UL to dropdown list not working
- How to add plugin with jQuery to custom theme?
- Manipulate retrieved ajax data in php
- Disable auto hide of the login form on Woocommerce’s checkout page
- How To Include JavaScript Files Only In Single Posts?
- WordPress Front-End Theme Editor
- Sorting dynamic table content with jQuery
- Determine image height within Fancybox
- How to use JQuery Marquee in WordPress?
- WordPress 3.2.1: jQuery is not defined, breaks Admin panel
- inner anchor scrolling with wordpress
- Bootstrap and jQuery error: $ is not defined
- Fire action after enqueue script
- Auto next upon clicking image without clicking Next button
- Doing Math with Gravity Forms List
- I am trying to load a Js Dynamic Audio Synth [closed]
- Uncaught ReferenceError: jQuery is not defined
- Remove button from page if viewed through Customizer
- Target a specific page/template in jQuery
- Multiple level category drop-down three levels is not working
- Add a jQuery snippet for sepcific user role
- JQuery and Javascript to pan image
- How to add JS snippet before closing tag?
- Show/Hide text after the first paragraph
- How to specify url for ajax call in wordpress plugin
- Dynamic image grid gallery
- Check with jQuery value of checkbox from custom admin panel
- JS script not being included [closed]
- Hide input field when second input field is in focus
- WooCommerce – PrettyPhoto appending URL with #prettyPhoto
- Are you sure popup on Contact Form 7 [closed]
- Is it possible to append an external html file to my wordpress navigation?
- jscrollpane problem with my child theme
- Using jQuery toggle() in WordPress post to show/hide Tweet button?
- Call a Simple JQuery from within Flash
- Change image on post title hover (more than one instance) – Jquery
- How to fire jQuery after $(window).on(‘load’)
- JQuery WordPress gallery [closed]
- My website si Blocked by google and warns the visitors that is Unsafe site
- Jquery in IE, fully messed up
- how to make this work with jquery 1.12.4 [closed]
- Theme variation conflict with jquery select2 dropdown
- Contact Form 7 – Submit Form not working After Ajax Request [closed]
- Making the Google Maps API non readable