Get selected element’s outer HTML

I believe that currently (5/1/2012), all major browsers support the outerHTML function. It seems to me that this snippet is sufficient. I personally would choose to memorize this: // Gives you the DOM element without the outside wrapper you want $(‘.classSelector’).html() // Gives you the outside wrapper as well only for the first element $(‘.classSelector’)[0].outerHTML … Read more

How to apply !important using .css()?

The problem is caused by jQuery not understanding the !important attribute, and as such fails to apply the rule. You might be able to work around that problem, and apply the rule by referring to it, via addClass(): .importantRule { width: 100px !important; } $(‘#elem’).addClass(‘importantRule’); Or by using attr(): $(‘#elem’).attr(‘style’, ‘width: 100px !important’); The latter … Read more

jQuery Ajax File Upload

File upload is not possible through AJAX. You can upload file, without refreshing page by using IFrame. You can check further details here. UPDATE With XHR2, File upload through AJAX is supported. E.g. through FormData object, but unfortunately it is not supported by all/old browsers. FormData support starts from following desktop browsers versions. IE 10+ … Read more

How can I remove all CSS classes using jQuery/JavaScript?

$(“#item”).removeClass(); Calling removeClass with no parameters will remove all of the item’s classes. You can also use (but it is not necessarily recommended. The correct way is the one above): $(“#item”).removeAttr(‘class’); $(“#item”).attr(‘class’, ”); $(‘#item’)[0].className=””; If you didn’t have jQuery, then this would be pretty much your only option: document.getElementById(‘item’).className=””;

Include jQuery in the JavaScript Console

Run this in your browser’s JavaScript console, then jQuery should be available… var jq = document.createElement(‘script’); jq.src = “https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js”; document.getElementsByTagName(‘head’)[0].appendChild(jq); // … give time for script to load, then type (or see below for non wait option) jQuery.noConflict(); NOTE: if the site has scripts that conflict with jQuery (other libs, etc.) you could still run … Read more

Changing the image source using jQuery

You can use jQuery’s attr() function. For example, if your img tag has an id attribute of ‘my_image’, you would do this: <img id=”my_image” src=”first.jpg” alt=”Insert link 1 alt text here” /> Then you can change the src of your image with jQuery like this: $(“#my_image”).attr(“src”,”second.jpg”); To attach this to a click event, you could … Read more

Adding options to a using jQuery?

Personally, I prefer this syntax for appending options: $(‘#mySelect’).append($(‘<option>’, { value: 1, text: ‘My option’ })); If you’re adding options from a collection of items, you can do the following: $.each(items, function (i, item) { $(‘#mySelect’).append($(‘<option>’, { value: item.value, text : item.text })); });

Ajax request returns 200 OK, but an error event is fired instead of success

jQuery.ajax attempts to convert the response body depending on the specified dataType parameter or the Content-Type header sent by the server. If the conversion fails (e.g. if the JSON/XML is invalid), the error callback is fired. Your AJAX code contains: dataType: “json” In this case jQuery: Evaluates the response as JSON and returns a JavaScript … Read more

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