Best way to use Google’s hosted jQuery, but fall back to my hosted library on Google fail

You can achieve it like this: <script src=”https://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js”></script> <script> window.jQuery || document.write(‘<script src=”/path/to/your/jquery”><\/script>’); </script> This should be in your page’s <head> and any jQuery ready event handlers should be in the <body> to avoid errors (although it’s not fool-proof!). One more reason to not use Google-hosted jQuery is that in some countries, Google’s domain name … Read more

Use jQuery to hide a DIV when the user clicks outside of it

Had the same problem, came up with this easy solution. It’s even working recursive: $(document).mouseup(function(e) { var container = $(“YOUR CONTAINER SELECTOR”); // if the target of the click isn’t the container nor a descendant of the container if (!container.is(e.target) && container.has(e.target).length === 0) { container.hide(); } });

How can I trigger the same function from multiple events with jQuery?

You can use .on() to bind a function to multiple events: $(‘#element’).on(‘keyup keypress blur change’, function(e) { // e.type is the type of event fired }); Or just pass the function as the parameter to normal event functions: var myFunction = function() { … } $(‘#element’) .keyup(myFunction) .keypress(myFunction) .blur(myFunction) .change(myFunction)

Best way to remove an event handler in jQuery?

jQuery ≥ 1.7 With jQuery 1.7 onward the event API has been updated, .bind()/.unbind() are still available for backwards compatibility, but the preferred method is using the on()/off() functions. The below would now be, $(‘#myimage’).click(function() { return false; }); // Adds another click event $(‘#myimage’).off(‘click’); $(‘#myimage’).on(‘click.mynamespace’, function() { /* Do stuff */ }); $(‘#myimage’).off(‘click.mynamespace’); jQuery … Read more

Selecting element by data attribute with jQuery

$(‘*[data-customerID=”22″]’); You should be able to omit the *, but if I recall correctly, depending on which jQuery version you’re using, this might give faulty results. Note that for compatibility with the Selectors API (document.querySelector{,all}), the quotes around the attribute value (22) may not be omitted in this case. Also, if you work with data … Read more

How can I get the ID of an element using jQuery?

The jQuery way: $(‘#test’).attr(‘id’) In your example: $(document).ready(function() { console.log($(‘#test’).attr(‘id’)); }); <script src=”https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js”></script> <div id=”test”></div> Or through the DOM: $(‘#test’).get(0).id; or even : $(‘#test’)[0].id; and reason behind usage of $(‘#test’).get(0) in JQuery or even $(‘#test’)[0] is that $(‘#test’) is a JQuery selector and returns an array() of results not a single element by its default … Read more

How do I format a Microsoft JSON date?

eval() is not necessary. This will work fine: var date = new Date(parseInt(jsonDate.substr(6))); The substr() function takes out the /Date( part, and the parseInt() function gets the integer and ignores the )/ at the end. The resulting number is passed into the Date constructor. I have intentionally left out the radix (the 2nd argument to … Read more

tech