Your problem = click propagation
Events in browser are being propagated along DOM element tree. You have to stop that in order for each call to individually execute. Your code also sets click handlers inline so I'd rather suggest you separate functionality from your markup.
Use jQuery
Instead of setting click handlers inline use jQuery:
$(".milestonehead img").click(function(evt) {
evt.preventDefault();
deletefun("35");
});
And similar for your H4
. Mind that my jQuery selector may not be appropriate in your situation but you can always distinguish particular elements by IDs or CSS classes.
Separation of concerns
Externalizing your functionality to code only file (or at least code block in your page) makes your page much more maintainable.
Set event handlers in DOM Ready event:
$(function(){
$(".milestonehead").click(function(evt) {
evt.preventDefault();
editfun("35");
});
$(".milestonehead img").click(function(evt) {
evt.preventDefault();
deletefun("35");
});
});
Improving markup
It seems that you have a list of some items (as you're doing something related to item 35) where each one is being represented by h4
and nested delete icon. Hence I'd rather change markup (and code) to do this:
<h4 class="milestonhead" data-edit-item="35">
Some header
<img src="/images/delete.gif" data-delete-item="35" />
</h4>
Style your elements using CSS file instead (separation of concerns again either in separate CSS file or style
block on your page) and you can thank me for this later.
.milestonehead {
cursor: pointer;
}
.milestonehead img {
float: right;
}
Code to handle these items would then look like this:
$(function() {
$("[data-edit-item]").click(function(evt){
evt.preventDefault();
editfun($(this).data("editItem"));
});
$("[data-delete-item]").click(function(evt){
evt.preventDefault();
deletefun($(this).data("deleteItem"));
});
});
This creates two common click handlers for any element that would have appropriate data attributes (data-edit-item
or data-delete-item
). So your header may have these as well some "Edit" link too. Just set appropriate values to that particular data attribute.