2

I have a JS code using on this page http://200tr.ru/app/admin/ When I press the cross sign, 2 events occur (blur and click), but only blur-code runs. I need contra, click-on-cross code. How can I do this?


$(function(){clickOnLink();});
    function clickOnLink(){
        $("span.link").on({
            click: function(){
        var linkId = $(this).text().replace(/\D+/g,"");
        var id = $(this).attr("id");
        var idNum = $(this).attr("id").replace(/\D+/g,"");
        var idType = "link_"+$(this).attr("id").replace(/\d+/g,"");
        var result = "<span class='link'>Переход на link</span> <input     type='text' id='"+id+"' value='"+linkId+"'> <span><span class='plus'>+</span><span    class='cross'>✘</span></span>";
        $(this).replaceWith(function(index, oldHTML){
        return result;
        });
        $("input").focus();
        clickOnCross();
        blurOnInput();
        }
    });
};
function clickOnCross(){
$("span.cross").on({
    click: function(){
        $(this).remove();
     }
});
};
function blurOnInput(){
$("input").on({
    blur: function(){
        $(this).prev().remove();
        $(this).next().remove();
        var idName = $(this).attr("id");
        var idNum = $(this).attr("id").replace(/\D+/g,"");
        var idType = "link_"+$(this).attr("id").replace(/\d+/g,"");
        $(this).replaceWith(function(index, newHTML){
        var linkId = $(this).val();
        var linkIdDb = "link"+$(this).val();
        $.get("handlers/send_link.php", { 
            id: idNum, 
            ans: idType,
            linkid: linkIdDb }
        );
        var result = "<span class='link' id='"+idName+"'>Переход на link"+linkId+". <a href='#link"+linkId+"'>⇗</a></span>";
        return result;
        });       
        clickOnLink();
    }
});
};
Jacques Marais
  • 2,666
  • 14
  • 33

1 Answers1

0

You remove the cross on the blur event which triggers on mousedown, way before a click event is invoked, and when you expect your click event to be triggered, there is no cross element anymore in DOM.

If you would like to bind something on the click event of your cross sign, don't remove it from DOM on the blur event of your input field.

Adam
  • 4,985
  • 2
  • 29
  • 61