Could anyone suggest performance improvements for the function I’ve written (below, javascript with bits of jquery)? Or point out any glaring, basic flaws? Essentially I have a javascript Google map and a set of list based results too, and the function is fired by a checkbox click, which looks at the selection of checkboxes (each identifying a ‘filter’) and whittles
Category: Questions
Define a global variable in a JavaScript function
Is it possible to define a global variable in a JavaScript function? I want use the trailimage variable (declared in the makeObj function) in other functions. Answer As the others have said, you can use var at global scope (outside of all functions and modules) to declare a global variable: (Note that that’s only true at global scope. If that
Why doesn’t JavaScript newlines work inside HTML?
I have the following: As you probably all know, n doesn’t work and I have to use <br> instead. It won’t work either if I link to an external .js file. Here are my questions: Why doesn’t n work? Why does <br> even work? Shouldn’t everything that’s inside the script tags be strictly JavaScript instead of a dirty mix between
Format a Number, Exactly Two in Length?
I have an integer that is less then 100 and is printed to an HTML page with JavaScript. How do I format the integer so that it is exactly two digits long? For example: 01 02 03 … 09 10 11 12 … Answer Update This answer was written in 2011. See liubiantao’s answer for the 2021 version. Original
How to keep an Javascript object/array ordered while also maintaining key lookups?
I have some data which I originally stored in a generic Javascript object, with the ID as a key: However, I discovered that browsers do not guarantee a particular object order when looping through them, so in the above “3” would come before “7”. I switched to using an array format like this: Now, I can loop in the correct
In JavaScript, is it possible save an on-click trigger and re-apply it later?
On a dynamic page I wish to disable and later re-enable sections. Visually they are grayed out, but need to prevent users from playing with the section until later. For elements using on-click triggers, would like to: save the current on-click trigger in an attribute remove the current on-click trigger add trigger that no-defaults and no-propagates to re-enable: get rid
Add characters to a string in Javascript
I need to add in a For Loop characters to an empty string. I know that you can use the function concat in Javascript to do concats with strings But it doesn’t work with my example. Any idea of how to do it in another way? My code : Answer
Change CSS of class in Javascript?
I’ve got a class with the display set to none I’d like to in Javascript now set it to inline I’m aware I can do this with an id with getElementById but what’s the cleanest way to do it with a class? Answer You can do that — actually change style rules related to a class — using the styleSheets array (MDN
JQuery Error: Uncaught TypeError: Object # has no method ‘ready’
my site is getting the error in this title in the javascript console. Google seems to say that it is because jquery isn’t loaded, but it is definitely visible in the head. the site is medical marijuana related, so nsfw for some.sorry for the messy head, it’s in dev mode. http://www.kindreviews.com/1/mmc/ Thanks, zeem Answer Apparently you are using both jQuery
Disable submit button on form submit
I wrote this code to disable submit buttons on my website after the click: Unfortunately, it doesn’t send the form. How can I fix this? EDIT I’d like to bind the submit, not the form 🙂 Answer Do it onSubmit(): What is happening is you’re disabling the button altogether before it actually triggers the submit event. You should probably also