javascript - How to get every char from a text inside an element using a loop -
*html
<div id="board_code" contenteditable="true"> <div>a b</div> <div>cd</div> </div>
*js
var inlinediv_num = $('#board_code > div ').size(); for( var x=0; x<inlinediv_num; x++ ){ var inlinediv_num_textl = $('#board_code > div ').eq(x).text().length; for( var y=0; y<inlinediv_num_textl; y++){ //problem here alert( $('#board_code > div ').eq(x).eq(y).text() ); }//for y }//for x
i expecting first alert 'a', 2nd ' ', 3rd 'c', 4th 'd'. use char comparison intellisense thingy.
note 2 loop structure should not compromised, need structure latter solving
check this fiddle.
it give characters 1 one in alert using substr
function.
edit: can use alert(inlinediv_num_text.charat(y));
function instead of substr
mentioned elisianopetrini.
var inlinediv_num = $('#board_code > div ').size(); (var x = 0; x < inlinediv_num; x++) { var inlinediv_num_textl = $('#board_code > div ').eq(x).text().length; var inlinediv_num_text = $('#board_code > div ').eq(x).text(); (var y = 0; y < inlinediv_num_textl; y++) { alert(inlinediv_num_text.substr(y, 1)); //modified } //for y } //for x
Comments
Post a Comment