I need to fetch all span values in a div into array or string
var tag_text_arr=$("#div_id span").each(function(){ return $(this).text(); });
Actually i need to fetch all span values inside div and want to create a string like this
span1_val|span2_val|span3_val|span4_val
If this is possible explain me...
This should output your 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 will start the numbering at span0
, not span1
. If you want it to start at span1
, use +(index + 1)+
instead of +index+
. (example)
spann_
prefixed to each element in the string, you can just 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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With