javascript - Counting number of span stored inside variable in jquery -
basically trying count number of spans stored inside items
variable length
returning 0
var items = '<br><span class="holiday">buddha jayanti</span><br><span class="workingday">shiva ratri</span>'; var length = $(items).find('span').length; alert(length); //returning 0
is there somthing missing here?
because span
@ root of element set, .find()
not return elements, need use .filter() instead
var items = '<br><span class="holiday">buddha jayanti</span><br><span class="workingday">shiva ratri</span>'; var length = $(items).filter('span').length; snippet.log(length);
<!-- provides `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
if there possibility have descendant span
elements then, option is
var items = '<br><span class="holiday">buddha jayanti</span><br><span class="workingday">shiva ratri</span><div><br><span class="workingday">some value</span></div>'; var length = $('<div />', { html: items }).find('span').length; snippet.log(length);
<!-- provides `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Comments
Post a Comment