Skip to content

Instantly share code, notes, and snippets.

@andykillen
Created June 18, 2016 14:16
Show Gist options
  • Save andykillen/82042febcf88853259e5a4880436ee94 to your computer and use it in GitHub Desktop.
Save andykillen/82042febcf88853259e5a4880436ee94 to your computer and use it in GitHub Desktop.
Always have a default, and always check before trying to use that the selector responds with something
// you can do this
$('.class-to-find').each(function(index){
outputValue = $(this).text();
});
// but it will fail badly if there is nothing found by $('class-to-find').
//better to do
outputValue = ''; // set a default value
if($('.class-to-find').length){ // make sure that there is something to loop
$('.class-to-find').each(function(index){ // do the loop
outputValue = $(this).text();// append the text
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment