web design and coding services with a huge twist of creativity.

Stuff Tagged ‘javascript’

jQuery tip: Check if an element exists

Published 2 years ago, in Blog, Web

It’s a very common question for people starting out with jQuery: How do I know if element X exists? There is no standard function for you to use, however, it’s really simple to write one yourself.

What you’ll probably try to do first is to perform a check on a jQuery object, like this:

//This won't work :(
if ( $("#myId") ) {
  //awesome code here
}

The reason this method doesn’t work is that when you use the $(), you’re asking for a jQuery object. You always get a jQuery object, even if it’s an empty one. Thus, the above code always returns true, and that’s just sad.

However, the object returned by jQuery will have an internal array filled with all the elements that do match your selector. You can access this internal list’s length using the .length property. This means that, if the length of your object is greater than zero, your element does indeed exist in the DOM. Putting that into code, we get:

//This works, yay!
if ( $("#myId").length > 0 ) {
  //awesome code here
}

Although the examples shown here only checked for the existence of an element with a certain ID attribute, you can obviously use this technique with any jQuery selector you wish.

labs.sapo Central Platform

Published 3 years ago, in Portfolio, Web

labs.sapo Central Platform

Project Timeframe

November 2008

Motivations

In order to support a growing number of partnerships between SAPO and Portuguese Universities, which resulted in the wide deployment of several lab.sapo Platforms, there was a need to develop a central platform that would aggregate all the content produced on the different platforms.
(…) more after the jump ›

External Links with Javascript

If you build websites and enjoy coding according to Web Standards, then you’re probably aware that the XHTML 1.0 Strict specification leaves out a rather common attribute: target, which is used inside <a> tags to specify how the hyperlink should behave. With a value of “_blank“, the hyperlink would open in a new browser window (or tab), which is recommended if the link navigates away from the current site. (…) more after the jump ›

Caixa d’Óculos

Published 3 years ago, in Portfolio

Project Timeframe

December 2007 – January 2007

Motivations

Caixa d’Óculos was the final project for a Dynamic Internet Technologies course, taken on the first year of my Masters in Multimedia Communication at the University of Aveiro.

This web application would allow for students in all degrees of teaching to manage their school related tasks, calendars and learning content in a shared, community space accessible from anywhere. It would differ from regular e-learning solutions in that it was strongly student oriented, and focused on those same students to publish and share educational content (videos, images, even their own notes) with others, while allowing everyone to view and rate those contents. (…) more after the jump ›

Bluespots Card Generator

Published 3 years ago, in Portfolio

Project Timeframe

September 2007 through November 2007

Motivations

The Bluespots Card Generator project was an interesting one. The application was to be designed using the Bluespots API, in development stage at the time, which allowed for Bluetooth communication between mobile devices and Bluetooth enabled Hubs (the Bluespots, which in turn communicated with a central server where all the data was stored). This essentially allowed for quick and seamless content distribution in event-type situations. (…) more after the jump ›