Reputation: 1782
I have a unique situation, my page looks like this:
As you can see, I can choose to copy one link or copy multiple link based on the checkbox I selected. I have two functions,one for Copy
button and another one for Copy selected URL
,where this function calls the first function.
// multiple copy
function copymultiplelink() {
var selectedFiles = $('.fileCheck:checked');
if(selectedFiles.length < 1 ){
alert("Please select at least one file.");
return false;
}
var filesList = [];
var $fileChecks = $('.fileCheck:checked');
$fileChecks.each(function() {
filesList.push($(this).val());
});
alert("You have copied " + filesList.length+ " URL");
copyURL(filesList);
}
// single copy
function copyURL(url) {
var copyText = url;
var el = document.createElement('textarea');
el.value = copyText;
el.setAttribute('readonly', '');
el.style = {
position: 'absolute',
left: '-9999px'
};
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
alert("You have copied the URL");
}
So,it's much easier for me to call the second function but when I click the Copy multiple button,I will receive two alerts,one from each function.
Is there a way that I can only show:
alert("You have copied " + filesList.length+ " URL");
when I select the copy selected url
button?
At the same time,I too need to call copyURL()
to copy all my selected URL.
Upvotes: 0
Views: 1149
Reputation: 1896
Do Like this:
// multiple copy
function copymultiplelink() {
var selectedFiles = $('.fileCheck:checked');
if(selectedFiles.length < 1 ){
alert("Please select at least one file.");
return false;
}
var filesList = [];
var $fileChecks = $('.fileCheck:checked');
$fileChecks.each(function() {
filesList.push($(this).val());
});
alert("You have copied " + filesList.length+ " URL");
copyURL(filesList, false);
}
// single copy
function copyURL(url, isAlertNeeded=true) {
var copyText = url;
var el = document.createElement('textarea');
el.value = copyText;
el.setAttribute('readonly', '');
el.style = {
position: 'absolute',
left: '-9999px'
};
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
isAlertNeeded && alert("You have copied the URL");
}
Upvotes: 2
Reputation: 421
You can simply add a parameter to copyUrl(url, showAlert)
and then copymultiplelink
can call copyUrl
with showAlert to false while your buttons call it with showAlert to true.
Don't forget to display alert based on showAlert value in copyUrl function :)
Code:
// multiple copy
function copymultiplelink() {
var selectedFiles = $('.fileCheck:checked');
if(selectedFiles.length < 1 ){
alert("Please select at least one file.");
return false;
}
var filesList = [];
var $fileChecks = $('.fileCheck:checked');
$fileChecks.each(function() {
filesList.push($(this).val());
});
alert("You have copied " + filesList.length+ " URL");
copyURL(filesList, false); // false to not show alert
}
// single copy
function copyURL(url, showAlert) {
var copyText = url;
var el = document.createElement('textarea');
el.value = copyText;
el.setAttribute('readonly', '');
el.style = {
position: 'absolute',
left: '-9999px'
};
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
if (showAlert) {
alert("You have copied the URL");
}
}
Upvotes: 1
Reputation: 1553
I hope I understand you well. You don't want to show alert when it was already shown by copymultiplelink()
. Then maybe call copyURL(url)
with a flag that tells copyURL(url)
to show or not to show msg.
function copyURL(url, showMsg) {
// your code
if (showMsg === true) {
alert("You have copied the URL");
}
}
and then inside copymultiplelink()
tell it to not show message
copyURL(filesList, false);
Upvotes: 2