Conditional Statement as Hierarchy jQuery

Is there a way to make jQuery use objects in a conditional statement as an object in a hierarchy. For Example, I want to validate that something exist then tell it to do something just using the this selector.

Like this

if ($(".tnImg").length) {

//i have to declare what object I am targeting here to get this to work

            $(this).animate({
            opacity: 0.5,   
            }, 200 );
  }

Is there a way to get jQuery to do this? I guess theres not a huge benefit but i still am curious

Answers


In jQuery, if nothing is found, then nothing happens, for example:

$(".tnImg").animate({ opacity: 0.5 }, 200 );

This won't error, if it doesn't find anything with class="tnImg" it simply won't run on any elements. This is one of the fundamentals that makes jQuery so terse :)

If you wanted to do lots with the object, this would let you use it as $(this) for each one:

$(".tnImg").each(function() {
  //$(this) is the current class="tnImg" element, this code runs for each one
  $(this).animate({ opacity: 0.5 }, 200 );
});

Need Your Help

open command line in c# and send commands PuTTy or KiTTy

c# command-line putty

I've searched all over the site and internet but couldn't find what I was looking for, any help will do, maybe I missed something...

Can you use Node Inspector with the .coffee handler?

node.js coffeescript node-debugger

Debugging node apps using node-inspector is pretty simple if you're scripting using JavaScript or compiled Coffee (coffee -c -m script.coffee).