Check if an element is defined

Posted on 2009-04-27
Last Modified: 2012-05-06
I have this code...

        var portfolio_item = $$('.portfolio_item');

Following is a bunch of stuff manipulating items within this  item, but if the item is not defined on the page, I want it to skip that stuff.
How can I do an if statement that makes sure this item is even on the page first?
Question by:BobCSD
    LVL 18

    Accepted Solution

    var portfolio_item = $$('.portfolio_item');
    if (portfolio_item)


    tried that?
    LVL 1

    Author Comment

    Everything I was reading was saying to check the length...
    if ($("#some").length)
    $('element').length == 0;
    And that errored.
    Thanks again! Yours works!
    LVL 1

    Author Closing Comment


    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question ( on how to make a page show some balloons animate up a page…
    Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now