javascript - Object doesn't support property or method 'slice' -
i'm newbie javascript/jquery world. have div several links , want collect url's .
then want extract href's last 9 characters (actually wish optimize , collect digits independently length @ end of each string).i tried extract them slice()
method not work.
in console error
object doesn't support property or method 'slice'
can convert object string ? appreciated ! code following
$(document).ready(function(){ var $posts= $('a.entry_title').each(function(){ $(this).attr('href'); }); var posts1 = $posts[0].slice(-9); var posts2 = $posts[1].slice(-9); var posts = ["myurl"+ posts1,"myurl"+posts2] $('#div1').load(posts[0] + " .shadow3"); $('#div2').load(posts[1] + " .shadow3"); }); </script>
you see object doesn't support because $.each returns jquery object.
use .map() instead because returns array on slice work
var $posts= $('a.entry_title').map(function(){ return $(this).attr('href'); });
result
["link1", "link2", "link3"....] // sample
if wish array of hrefs last 9 characters of each link can use map way
var $posts= $('a.entry_title').map(function(){ return $(this).attr('href').slice(-9); // or can own magic });
result this
["k1", "k2", "k3"....] // after slicing words
Comments
Post a Comment