Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Converting jQuery code to pure JavaScript

    Hi,

    I have the following functions that is coded in jQuery and I want to convert it to pure JavaScript if possible.

    Code:
    $(document).mouseup(function (e) {
    	var container = $('#box');
    	if (!container.is(e.target) && container.has(e.target).length === 0) {
    		...
    	}
    });
    Thanks for any ideas.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Try this:
    Code:
    document.onmouseup = function(e) {
        e = e || window.event;
        var container = document.getElementById('box'),
            target = e.target || e.srcElement;
    
        if (container !== target && !has(container, target)) {
            alert('here!');
        }
    }
    
    function has(container, target) {
        if (target === window || target === document || target === document.documentElement || (target === document.body && (container === document || container === document.documentElement))) {
            return false;
        }
    
        var descendants = container.getElementsByTagName(target.tagName);
        for (var i=0; i < descendants.length; i++) {
            if (descendants[i] === target) {
                return true;
            }
        }
        return false;
    }

  • #3
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,302
    Thanks
    28
    Thanked 276 Times in 270 Posts
    Quote Originally Posted by envayo View Post
    I have the following functions that is coded in jQuery and I want to convert it to pure JavaScript if possible.

    Code:
    $(document).mouseup(function (e) {
    	var container = $('#box');
    	if (!container.is(e.target) && container.has(e.target).length === 0) {
    		...
    	}
    });
    Assuming I understand what you're trying to do, this code should do the trick:

    Code:
    function doSomething() {
    	// do something
    }
    function stopExternalEventPropagation(endClickEvent) {
    	endClickEvent.stopPropagation();
    }
    document.addEventListener("mouseup", doSomething);
    document.getElementById("box").addEventListener("mouseup", stopExternalEventPropagation);
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #4
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you both! Both scripts did the job. My purpose was to close #box element when outside is clicked.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •