If I have this JS object literal: How can I create a new, sorted object literal: Answer Re: How to sort a JS Object? Answer: You can’t. So instead, you need a more sophisticated data structure. You have many options: You can use a separate array to hold the order of the object’s keys. (This is what @Felix Kling’s answer
Tag: javascript
How do you use the ? : (conditional) operator in JavaScript?
What is the ?: (question mark and colon operator aka. conditional or “ternary”) operator and how can I use it? Answer This is a one-line shorthand for an if-else statement. It’s called the conditional operator.1 Here is an example of code that could be shortened with the conditional operator: This can be shortened with the ?: like so: Like all
JS Busy loading indicator ignore middle click
My busy loading indicator basically works by detecting clicks. However, I just noted that when I middle click an item, it opens a link in a new tab and then the loading indicator shows up forever. How can I tell JS to ignore the middle mouse button? Answer You can try to, but it won’t work very well with all
Any way to generate javascript or pseudo code for a Data Flow?
I have a web form where users answer questions via dropdowns, and I wish to make other questions available based on their responses. e.g. if your business has a premises then ask about opening hours. If they don’t, show the regions they operate in. With each question I add to the process, the more complex the code becomes. A lot
How to generate short uid like “aX4j9Z” (in JS)
For my web application (in JavaScript) I want to generate short guids (for different objects – that are actually different types – strings and arrays of strings) I want something like “aX4j9Z” for my uids (guids). So these uids should be lightweight enough for web transfer and js string processing and quite unique for not a huge structure (not more
How to know if two arrays have the same values
I have these two arrays: one is filled with information from an ajax request and another stores the buttons the user clicks on. I use this code (I filled with sample numbers): But it always gives false, even if the two arrays are the same, but with different name. (I checked this in Chrome’s JS Console). So, is there any
Upload file with Ajax XMLHttpRequest
I am trying to send file with XMLHttpRequest with this code. I get this error: T The request was rejected because no multipart boundary was found. What am I doing wrong? Answer There is no such thing as xhr.file = file;; the file object is not supposed to be attached this way. xhr.send(file) doesn’t send the file. You have to
RegEx to match stuff between parentheses
I’m having a tough time getting this to work. I have a string like: And I need regex or a method of getting each match between the parentheses and return an array of matches like: The regex I’m using is /((.+))/ which does seem to match the right thing if there is only one set of parenthesis. How can I
Problems with “this” in JavaScript namespaces and event listeners
First of all, I’m attempting to use faux namespaces in my JavaScript program like so: I’m not crazy about using this to call other functions, but up to recently, it has worked. However, I’ve added event listeners to some elements, and they interpret this to be the target object. I know I can use the full namespace instead of this
How to simulate a mouse click using JavaScript?
I know about the document.form.button.click() method. However, I’d like to know how to simulate the onclick event. I found this code somewhere here on Stack Overflow, but I don’t know how to use it 🙁 How do I fire a mouse click event using JavaScript? Answer (Modified version to make it work without prototype.js) You can use it like this: Note