Created
June 18, 2016 14:16
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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