jquery - Need to fetch all span values in a div as an array or string? -
i need fetch span values in div array or string
var tag_text_arr=$("#div_id span").each(function(){ return $(this).text(); });
actually need fetch span values inside div , want create string
span1_val|span2_val|span3_val|span4_val
if possible explain me...
this should output required string:
var arr = []; $("#div_id span").each(function(index, elem){ arr.push("span" +index+ "_" + $(this).text()); }); return arr.join("|");
working demo: http://jsfiddle.net/hmuub/
this start numbering @ span0
, not span1
. if want start @ span1
, use +(index + 1)+
instead of +index+
. (example)
if read question wrong (and don't want
spann_
prefixed each element in string, can use jquery's $.map()
function: var tag_text_arr = $.map($("#div_id span"), function(elem, index){ return $(elem).text(); }).join("|"); return tag_text_arr;
working demo: http://jsfiddle.net/bqlj6/
Comments
Post a Comment