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(); } });

HTML text input allow only numeric input

Note: This is an updated answer. Comments below refer to an old version which messed around with keycodes. JavaScript Try it yourself on JSFiddle. You can filter the input values of a text <input> with the following setInputFilter function (supports Copy+Paste, Drag+Drop, keyboard shortcuts, context menu operations, non-typeable keys, the caret position, different keyboard layouts, … Read more

jQuery AJAX submit form

This is a simple reference: // this is the id of the form $(“#idForm”).submit(function(e) { e.preventDefault(); // avoid to execute the actual submit of the form. var form = $(this); var actionUrl = form.attr(‘action’); $.ajax({ type: “POST”, url: actionUrl, data: form.serialize(), // serializes the form’s elements. success: function(data) { alert(data); // show response from the … Read more

Getting the ID of the element that fired an event

In jQuery event.target always refers to the element that triggered the event, where event is the parameter passed to the function. http://api.jquery.com/category/events/event-object/ $(document).ready(function() { $(“a”).click(function(event) { alert(event.target.id); }); }); Note also that this will also work, but that it is not a jQuery object, so if you wish to use a jQuery function on it … Read more

jQuery’s jquery-1.10.2.min.map is triggering a 404 (Not Found)

If Chrome DevTools is reporting a 404 for a .map file (maybe jquery-1.10.2.min.map, jquery.min.map or jquery-2.0.3.min.map, but can happen with anything) first thing to know is this is only requested when using the DevTools. Your users will not be hitting this 404. Now you can fix this or disable the sourcemap functionality. Fix: get the … Read more

Selecting and manipulating CSS pseudo-elements such as ::before and ::after using javascript (or jQuery)

You could also pass the content to the pseudo element with a data attribute and then use jQuery to manipulate that: In HTML: <span>foo</span> In jQuery: $(‘span’).hover(function(){ $(this).attr(‘data-content’,’bar’); }); In CSS: span:after { content: attr(data-content) ‘ any other text you may want’; } If you want to prevent the ‘other text’ from showing up, you … Read more

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)

Is Safari on iOS 6 caching $.ajax results?

After a bit of investigation, turns out that Safari on iOS6 will cache POSTs that have either no Cache-Control headers or even “Cache-Control: max-age=0”. The only way I’ve found of preventing this caching from happening at a global level rather than having to hack random querystrings onto the end of service calls is to set … Read more

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)