Search code examples
javascriptjqueryevent-bubbling

On click of textbox event getting bubbled


This is my HTML structure.

<div id="dvHirearachy" class="MarginTB10">
    <span>
        <label>Hierarchy Names</label><br />
        <input type="text" name="txtHierarchy" />
        <a id="ancRemove" href="#">Remove</a>
    </span><br />
    <a id="ancNew" href="#">New Hierarchy Name</a>
</div>

On click of anchor tag "ancNew" , I am generating again the complete span tag above mentioned in the markup.

The problem is on click of textbox also the span structure is getting generated. Same problem i was facing on click of "ancRemove" for that i tried to stop the event bubbling, it has worked for this but not for the textbox.

my script.

 $(document).ready(function () {

            $("#ancRemove").click(function (e) {
                RemoveHierarchy(this, e);
            });

            $("#ancNew").click(function (e) {
                generateNewHierarchy(e);
            });
});
 function generateNewHierarchy(e) {
            if (window.event) {
                var e = window.event;
                e.cancelBubble = true;
            } else {
                e.preventDefault();
                e.stopPropagation();
                var container = createElements('span', null);
                $(container).append(createElements('input', 'text'));
                $(container).append(createElements('a', null));
                $(container).append("<br/>").prependTo("#ancNew");
                $(container).children('input[type=text]').focus();
            }
        }

        function createElements(elem,type) {
            var newElem = document.createElement(elem);

            if (type != null) {
                newElem.type = "input";
                newElem.name = "txtHierarchy";
                $(newElem).addClass('width_medium');
            }

            if (elem == "a") {
                newElem.innerHTML = "Remove";
                $(newElem).click(function (e) {
                    RemoveHierarchy(this,e);
                });
            }
            return newElem;
        }

        function RemoveHierarchy(crntElem, e) {
            e.stopPropagation();
            $(crntElem).parents("span:first").remove();
        }

what is the way to avoid the situation.


Solution

  • Check this jsfiddle :

    http://jsfiddle.net/xemhK/

    Issue is prepandTo statement, It is prepading the elements in #ancNew anchor tag, thats why all textbox and remove anchor, are propagating click event of #ancNew, and it is calling generateNewHierarchy() function.

    Change in $(container).append("<br/>").prepandTo("#ancNew"); to $(container).append("<br/>").insertBefore("#ancNew");

    function generateNewHierarchy(e) {
        if (window.event) {
            var e = window.event;
            e.cancelBubble = true;
        } else {
            e.preventDefault();
            e.stopPropagation();
            var container = createElements('span', null);
            $(container).append(createElements('input', 'text'));
            $(container).append(createElements('a', null));
    
            //$(container).append("<br/>").prepandTo("#ancNew");
            $(container).append("<br/>").insertBefore("#ancNew");
    
            $(container).children('input[type=text]').focus();
        }
    }
    

    and in createElements

    if (elem == "a") {
        newElem.innerHTML = "Remove";
        $(newElem).attr("href","#").click(function (e) {
            RemoveHierarchy(this,e);
        });
    }