Skip to content

Commit 1ff15fc

Browse files
committed
Added copy to clipboard functionality
1 parent 5848e26 commit 1ff15fc

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

public/javascripts/shorten.js

+21
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
$(document).ready(function(){
2+
$('[data-toggle="tooltip"]').tooltip();
3+
});
4+
5+
$('#clipboardTooltip').tooltip({placement: 'bottom',trigger: 'manual'});
6+
17
$('#short').submit(function(event){
28
var urlText = $('#urlField').val();
39
var config = {
@@ -8,8 +14,23 @@ $('#short').submit(function(event){
814
.then(function (response) {
915
var shortUrl = response.data;
1016
$('#shortUrl').text(shortUrl);
17+
$('#clipboardTooltip').tooltip('hide')
18+
.attr('data-original-title', 'Copy to Clipboard')
19+
.tooltip('show');
1120
})
1221
.catch(function (error) {
1322
console.log(error);
1423
});
1524
});
25+
26+
function clipboardCopy() {
27+
var copyText = document.getElementById("shortUrl").innerText;
28+
var $temp = $("<input>");
29+
$("body").append($temp);
30+
$temp.val(copyText).select();
31+
document.execCommand("copy");
32+
$temp.remove();
33+
$('#clipboardTooltip').tooltip('hide')
34+
.attr('data-original-title', 'Link Copied!')
35+
.tooltip('show');
36+
}

views/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ <h5>SlashURL</h5>
3131
<button type="submit" class="btn btn-primary">Shorten It!</button>
3232
</form>
3333
<br>
34-
<i><span id="shortUrl"></span></i>
34+
<i id="clipboardTooltip" rel="tooltip" data-original-title="Copy to Clipboard"><span onclick="clipboardCopy()" id="shortUrl"></span></i>
3535
</div>
3636
</div>
3737
</div>

0 commit comments

Comments
 (0)