Reputation: 31
I have a file listing and there is option to delete the file. So on clicking the delete it asks for a confirmation. On clicking the confirm the file is deleted. Please check this jsfiddle for that
When I add a html click handler inorder to change the confirm text to the original delete text,
$('html').click(function(event){ if($('.confirmDelete').is(':visible')){ $('.confirmDelete').hide(); $('.deleteLink').show(); return false; } }); $('.confirmDelete').hide(); $('.deleteLink').click(function (event) { $(this).hide(); $('.confirmDelete').filter(':visible').each(function () { $(this).hide(); $(this).prev('.deleteLink').show(); }); $(this).next('.confirmDelete').show(); event.stopPropagation(); return false; });
But the issue is now on clicking the 'Confirm' text it is not going to href but hiding the confirm text and showing the delete text.
Upvotes: 0
Views: 48
Reputation: 31
I was able to update the script and it is now working as intended
$('html').click(function(e){
var target = $(e.target);
if(!target.hasClass('confirmDelete')){
if($('.confirmDelete').is(':visible')){
$('.confirmDelete').hide();
$('.deleteLink').show();
return false;
}
}
});
$('.confirmDelete').hide();
$('.deleteLink').click(function (event) {
$(this).hide();
$('.confirmDelete').filter(':visible').each(function () {
$(this).hide();
$(this).prev('.deleteLink').show();
});
$(this).next('.confirmDelete').show();
event.stopPropagation();
return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table width="100%">
<tr>
<td width="60%">file name 1</td>
<td width="40%">
<a class="deleteLink" href="#">Del</a>
<a style="display:none;" class="confirmDelete" href="delete.html?file=1">Confirm?</a>
</td>
</tr>
<tr>
<td>file name 2</td>
<td>
<a class="deleteLink" href="#">Del</a>
<a style="display:none;" class="confirmDelete" href="delete.html?file=2">Confirm?</a>
</td>
</tr>
<tr>
<td>file name 3</td>
<td>
<a class="deleteLink" href="#">Del</a>
<a style="display:none;" class="confirmDelete" href="http://google.com?file=3">Confirm?</a>
</td>
</tr>
<tr>
<td>file name 4</td>
<td>
<a class="deleteLink" href="#">Del</a>
<a style="display:none;" class="confirmDelete" href="http://google.com?file=4">Confirm?</a>
</td>
</tr>
<tr>
<td>file name 5</td>
<td>
<a class="deleteLink" href="#">Del</a>
<a style="display:none;" class="confirmDelete" href="http://google.com?file=5">Confirm?</a>
</td>
</tr>
</table>
Upvotes: 0
Reputation: 167172
Try instead of html
, document
.
$(document).not(".confirmDelete").click(function(event){
if($('.confirmDelete').is(':visible')){
$('.confirmDelete').hide();
$('.deleteLink').show();
return false;
}
});
$('.confirmDelete').hide();
$('.deleteLink').click(function (event) {
$(this).hide();
$('.confirmDelete').filter(':visible').each(function () {
$(this).hide();
$(this).prev('.deleteLink').show();
});
$(this).next('.confirmDelete').show();
event.stopPropagation();
return false;
});
Fiddle: https://jsfiddle.net/945j4g0x/
Upvotes: 1