jquery - Why isn't JavaScript for loop incrementing? -
i'm trying replace <li>
1. 2. 3. respectively. managed change <li>
number, number 0
. loop doesn't want work. honest, method may impossible.
take @ fiddle if you'd like.
this function(){...}
:
function doit(){ var input = document.getelementbyid("input"); var li = /<li>/; // match opening li var lib = /<\/li>/; // match closing li var numberofitems = input.value.match(li).length; // number of lis occur for(var = 0; < numberofitems; i++) { insertnumber(i); // execute insertnumber function w/ parameter of incremented } function insertnumber(number){ input.value = input.value.replace(li, number + "." + " ").replace(lib, ""); } }
i understand insertnumber(){...}
function is not necessary.
here's alternative method, turning html textarea contents dom elements jquery can manipulate , managing them way:
function doit() { var $domelements = $.parsehtml( $('#input').val().trim() ), output = [], = 1; $.each($domelements, function(index, element) { if($(this).text().trim() != '') { output.push( + '. ' + $(this).text().trim() ); i++; } }); $('#input').val(output.join('\n')); }
Comments
Post a Comment