Questions tagged [jquery]

jQuery is a JavaScript library. jQuery is a popular cross-browser JavaScript library that facilitates Document Object Model (DOM) traversal, event handling, animations, and AJAX interactions by minimizing the discrepancies across browsers. A question tagged jQuery should be related to jQuery, so jQuery should be used by the code in question, and at least jQuery usage-related elements must be in the question. Consider also adding the JavaScript tag.

Filter by
Sorted by
Tagged with
1675 votes
23 answers
1.1m views

Event binding on dynamically created elements?

I have a bit of code where I am looping through all the select boxes on a page and binding a .hover event to them to do a bit of twiddling with their width on mouse on/off. This happens on page ready ...
Eli's user avatar
  • 97.9k
601 votes
7 answers
196k views

Why does jQuery or a DOM method such as getElementById not find the element?

What are the possible reasons for document.getElementById, $("#id") or any other DOM method / jQuery selector not finding the elements? Example problems include: jQuery silently failing to ...
Felix Kling's user avatar
3288 votes
15 answers
6.2m views

Why does my JavaScript code receive a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error, while Postman does not?

Mod note: This question is about why XMLHttpRequest/fetch/etc. on the browser are subject to the Same Access Policy restrictions (you get errors mentioning CORB or CORS) while Postman is not. This ...
Mr Jedi's user avatar
  • 34k
4506 votes
15 answers
853k views

"Thinking in AngularJS" if I have a jQuery background? [closed]

Suppose I'm familiar with developing client-side applications in jQuery, but now I'd like to start using AngularJS. Can you describe the paradigm shift that is necessary? Here are a few questions that ...
Mark Rajcok's user avatar
3140 votes
34 answers
1.5m views

How can I upload files asynchronously with jQuery?

I would like to upload a file asynchronously with jQuery. $(document).ready(function () { $("#uploadbutton").click(function () { var filename = $("#file").val(); $.ajax({...
Sergio del Amo's user avatar
7702 votes
58 answers
7.8m views

How do I redirect to another webpage?

How can I redirect the user from one page to another using jQuery or pure JavaScript?
venkatachalam's user avatar
2491 votes
18 answers
722k views

.prop() vs .attr()

So jQuery 1.6 has the new function prop(). $(selector).click(function(){ //instead of: this.getAttribute('style'); //do i use: $(this).prop('style'); //or: $(this).attr('style'...
Naftali's user avatar
  • 145k
1137 votes
28 answers
883k views

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

Is there any way to select/manipulate CSS pseudo-elements such as ::before and ::after (and the old version with one semi-colon) using jQuery? For example, my stylesheet has the following rule: .span::...
JBradwell's user avatar
  • 11.5k
862 votes
27 answers
1.8m views

jQuery Ajax File Upload

Can I use the following jQuery code to perform file upload using POST method of an ajax request ? $.ajax({ type: "POST", timeout: 50000, url: url, data: dataString, success: ...
Willy's user avatar
  • 9,751
758 votes
17 answers
1.8m views

jQuery Ajax POST example with PHP

I am trying to send data from a form to a database. Here is the form I am using: <form name="foo" action="form.php" method="POST" id="foo"> <label for="bar">A bar</label> &...
Thew's user avatar
  • 15.8k
3242 votes
14 answers
935k views

event.preventDefault() vs. return false

When I want to prevent other event handlers from executing after a certain event is fired, I can use one of two techniques. I'll use jQuery in the examples, but this applies to plain-JS as well: 1. ...
RaYell's user avatar
  • 69.9k
2925 votes
91 answers
1.7m views

How do I detect a click outside an element?

I have some HTML menus, which I show completely when a user clicks on the head of these menus. I would like to hide these elements when the user clicks outside the menus' area. Is something like this ...
1946 votes
65 answers
1.9m views

How to detect a mobile device using jQuery

Is there a way to detect whether or not a user is using a mobile device in jQuery? Something similar to the CSS @media attribute? I would like to run a different script if the browser is on a handheld ...
2346 votes
54 answers
3.0m views

Remove duplicate values from JS array [duplicate]

I have a very simple JavaScript array that may or may not contain duplicates. var names = ["Mike","Matt","Nancy","Adam","Jenny","Nancy",&...
kramden88's user avatar
  • 23.7k
466 votes
5 answers
357k views

Is there a JavaScript / jQuery DOM change listener?

Essentially I want to have a script execute when the contents of a DIV change. Since the scripts are separate (content script in the Chrome extension & webpage script), I need a way simply observe ...
Fletcher Moore's user avatar
1377 votes
47 answers
913k views

How to check if element is visible after scrolling?

I'm loading elements via AJAX. Some of them are only visible if you scroll down the page. Is there any way I can know if an element is now in the visible part of the page?
yoavf's user avatar
  • 21k
628 votes
8 answers
604k views

How do I attach events to dynamic HTML elements with jQuery? [duplicate]

Suppose I have some jQuery code that attaches an event handler to all elements with class .myclass. For example: $(function(){ $(".myclass").click( function() { // do something }); }...
frankadelic's user avatar
  • 20.6k
1761 votes
58 answers
1.1m views

Convert form data to JavaScript object with jQuery

How do I convert all elements of my form to a JavaScript object? I'd like to have some way of automatically building a JavaScript object from my form, without having to loop over each element. I do ...
2117 votes
30 answers
1.8m views

Preview an image before it is uploaded

I want to be able to preview a file (image) before it is uploaded. The preview action should be executed all in the browser without using Ajax to upload the image. How can I do this?
Simbian's user avatar
  • 21.4k
492 votes
26 answers
1.0m views

Download a file by jQuery.Ajax

I have a Struts2 action in the server side for file downloading. <action name="download" class="com.xxx.DownAction"> <result name="success" type="stream"> <param name="...
hguser's user avatar
  • 35.3k
578 votes
37 answers
2.2m views

JQuery - $ is not defined

I have a simple jquery click event <script type="text/javascript"> $(function() { $('#post').click(function() { alert("test"); }); }); &...
Paul Creasey's user avatar
  • 28.5k
338 votes
13 answers
387k views

Add a property to a JavaScript object using a variable as the name?

I'm pulling items out of the DOM with jQuery and want to set a property on an object using the id of the DOM element. Example const obj = {} jQuery(itemsFromDom).each(function() { const element =...
Todd R's user avatar
  • 18.3k
857 votes
15 answers
759k views

How can I access the contents of an iframe with JavaScript/jQuery?

I would like to manipulate the HTML inside an iframe using jQuery. I thought I'd be able to do this by setting the context of the jQuery function to be the document of the iframe, something like: $(...
rz.'s user avatar
  • 19.9k
2412 votes
38 answers
1.3m views

$(document).ready equivalent without jQuery

I have a script that uses $(document).ready, but it doesn't use anything else from jQuery. I'd like to lighten it up by removing the jQuery dependency. How can I implement my own $(document).ready ...
FlySwat's user avatar
  • 173k
118 votes
4 answers
96k views

Persist variables between page loads

I am trying to capture the submit button press of my form and if the form is submitted, the page refreshes and I show a few hidden fields. I would like to capture whether the form has been submitted ...
Neophile's user avatar
  • 5,720
2695 votes
33 answers
3.1m views

Scroll to an element with jQuery

I have this input element: <input type="text" class="textfield" value="" id="subject" name="subject"> Then I have some other elements, like ...
DiegoP.'s user avatar
  • 45.3k
628 votes
13 answers
1.1m views

Sending multipart/formdata with jQuery.ajax

I've got a problem sending a file to a serverside PHP-script using jQuery's ajax-function. It's possible to get the File-List with $('#fileinput').attr('files') but how is it possible to send this ...
zoku's user avatar
  • 7,106
1873 votes
10 answers
2.2m views

Vanilla JavaScript equivalent of jQuery's $.ready() - how to call a function when the page/DOM is ready for it [duplicate]

With jQuery, we all know the wonderful .ready() function: $('document').ready(function(){}); However, let's say I want to run a function that is written in standard JavaScript with no library ...
chris's user avatar
  • 36.3k
828 votes
9 answers
1.7m views

SecurityError: Blocked a frame with origin from accessing a cross-origin frame

I am loading an <iframe> in my HTML page and trying to access the elements within it using JavaScript, but when I try to execute my code, I get the following error: SecurityError: Blocked a ...
mubashermubi's user avatar
  • 8,946
1987 votes
18 answers
705k views

Abort Ajax requests using jQuery

Is it possible that using jQuery, I cancel/abort an Ajax request that I have not yet received the response from?
lukewm's user avatar
  • 21.5k
1349 votes
18 answers
1.5m views

How do I set/unset a cookie with jQuery?

How do I set and unset a cookie using jQuery, for example create a cookie named test and set the value to 1?
omg's user avatar
  • 137k
1088 votes
21 answers
2.6m views

jQuery AJAX submit form

I have a form with name orderproductForm and an undefined number of inputs. I want to do some kind of jQuery.get or ajax or anything like that that would call a page through Ajax, and send along all ...
Nathan H's user avatar
  • 48.2k
4573 votes
44 answers
3.8m views

Setting "checked" for a checkbox with jQuery

I'd like to do something like this to tick a checkbox using jQuery: $(".myCheckBox").checked(true); or $(".myCheckBox").selected(true); Does such a thing exist?
tpower's user avatar
  • 56.3k
834 votes
40 answers
3.0m views

Uncaught ReferenceError: $ is not defined?

How come this code throws an Uncaught ReferenceError: $ is not defined when it was OK before? $(document).ready(function() { $('#tabs > ul').tabs({ fx: { opacity: 'toggle' } }); $('#...
Olivers's user avatar
  • 8,549
492 votes
15 answers
819k views

jQuery AJAX cross domain

Here are two pages, test.php and testserver.php. test.php <script src="scripts/jq.js" type="text/javascript"></script> <script> $(function() { $.ajax({...
Firose Hussain's user avatar
431 votes
25 answers
826k views

Detect browser or tab closing

Is there any cross-browser JavaScript/jQuery code to detect if the browser or a browser tab is being closed, but not due to a link being clicked?
cometta's user avatar
  • 35.2k
1531 votes
34 answers
1.1m views

How to manage a redirect request after a jQuery Ajax call

I'm using $.post() to call a servlet using Ajax and then using the resulting HTML fragment to replace a div element in the user's current page. However, if the session times out, the server sends a ...
Elliot Vargas's user avatar
1387 votes
17 answers
1.2m views

window.onload vs $(document).ready()

What are the differences between JavaScript's window.onload and jQuery's $(document).ready() method?
Vaibhav Jain's user avatar
568 votes
20 answers
648k views

Click event doesn't work on dynamically generated elements [duplicate]

<html> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> $(document).ready(function() { $("...
cheng's user avatar
  • 6,626
560 votes
31 answers
1.7m views

How to get client's IP address using JavaScript? [closed]

I need to somehow retrieve the client's IP address using JavaScript; no server side code, not even SSI. However, I'm not against using a free 3rd party script/service.
FlySwat's user avatar
  • 173k
465 votes
8 answers
324k views

Can I use multiple versions of jQuery on the same page?

A project I'm working on requires the use of jQuery on customers' Web pages. Customers will insert a chunk of code that we'll supply which includes a few <script> elements that build a widget in ...
Bungle's user avatar
  • 19.5k
273 votes
5 answers
158k views

jQuery Mobile: document ready vs. page events

I am using jQuery Mobile, and I am having trouble understanding differences between classic document ready and jQuery Mobile page events. What is the real difference? Why should <!-- language: ...
user2001897's user avatar
  • 3,647
699 votes
20 answers
488k views

Preloading images with jQuery

I'm looking for a quick and easy way to preload images with JavaScript. I'm using jQuery if that's important. I saw this here (http://nettuts.com...): function complexLoad(config, fileNames) { for ...
user avatar
5172 votes
68 answers
4.8m views

How do I check whether a checkbox is checked in jQuery?

I need to check the checked property of a checkbox and perform an action based on the checked property using jQuery. For example, if the age checkbox is checked, then I need to show a textbox to enter ...
2155 votes
42 answers
805k views

How do I format a Microsoft JSON date?

I'm taking my first crack at Ajax with jQuery. I'm getting my data onto my page, but I'm having some trouble with the JSON data that is returned for Date data types. Basically, I'm getting a string ...
3135 votes
47 answers
862k views

Is there an "exists" function for jQuery?

How can I check the existence of an element in jQuery? The current code that I have is this: if ($(selector).length > 0) { // Do something } Is there a more elegant way to approach this? ...
Jake McGraw's user avatar
  • 55.7k
772 votes
26 answers
659k views

HTML-encoding lost when attribute read from input field

I’m using JavaScript to pull a value out from a hidden field and display it in a textbox. The value in the hidden field is encoded. For example, <input id='hiddenId' type='hidden' value='chalk &...
AJM's user avatar
  • 32.2k
405 votes
12 answers
185k views

How do I select text nodes with jQuery?

I would like to get all descendant text nodes of an element, as a jQuery collection. What is the best way to do that?
Christian Oudard's user avatar
1069 votes
40 answers
908k views

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

I am using this code: $('body').click(function() { $('.form_wrapper').hide(); }); $('.form_wrapper').click(function(event){ event.stopPropagation(); }); And this HTML: <div class="...
Scott Yu - builds stuff's user avatar
326 votes
14 answers
1.1m views

How to get full path of selected file on change of <input type=‘file’> using javascript, jquery-ajax?

How to get full path of file while selecting file using <input type=‘file’> <input type="file" id="fileUpload"> <script type="text/javascript"> function getFilePath(){ $('input[...
Yogesh Pingle's user avatar

1
2 3 4 5
1583