Archive for category ‘Javascript’

2010
10
Apr
Category: Javascript, jQuery

Using document.getElementById(“…”) in plain ol’ Javascript will return a reference the DOM element by the given name. In jQuery, we use $ for everything. So I assumed using $(“#…”) would be the same thing. This is wrong. $(“#…”) will return an associative array (object) which is what allows us to change properties of the DOM element. If I am looking to return the reference to the DOM element alone in jQuery, then you need to use $(“#…”).get(0) which will return the first instance of that object. The zero is important because .get() return an array. So if you have more than one instance of what you are looking for, you can target a specific instance of it.

If you can’t bring yourself to use .get() you can also use $(“#…”)[0] which would do the same thing.

2009
14
Jun
Category: Javascript
Tags:

Generally random number functions pick a number between 0 and another number. Every couple of months I run into a case where I need to pick a random number between two numbers. For example I need a random number between 5 and 10. I always seems to have a tough time finding one online for some reason so I am posting this in the hopes it will help someone else or even me when I need to use it again later.

This function also allows you to specify the decimal places for your random number.

function randomBetween(minV, maxV, floatV)<br>{<br>	var randV = minV + (Math.random() * (maxV - minV));<br>	return typeof floatV == 'undefined' ? Math.round(randV) : rand.toFixed(floatV);<br>}

You can use something similar with Flash as well. I have a class that extends the Math class but I haven’t uploaded it yet.

2009
04
May
Category: Javascript, Links

I recently found FancyZoom 1.1 which is the Mac-esque version of Lightbox or Thickbox. I like it and I think it has a lot of potential. The pros and the cons:

Pros:

  1. It doesn’t black out the entire screen to display the image.
  2. Allows you to be able to click on other images or links that are on the screen while the image is open.
  3. Very much styled after the Mac.

Cons:

  1. Only works with images.
  2. All image links on the page will display using the zoom, not specific ones with a CSS or piece of Javascript attached to the link.
  3. Every once in a while the loading image doesn’t display and you get a strange white box with the ‘X’ in the top left corner because either the image can’t load or hasn’t loaded.
  4. Very much styled after the Mac.

I wonder what would happen to it is you linked to an image that wasn’t there? Does anyone know what would happen? I’m too lazy to find out for myself. :)