javascript - JS/jQuery Replacing Part Of Date String Not Working -


i have visible table row containing table cell class 'time-col'. in fact have more 1 , visibility of parent rows dynamic.

i attempting replace 3 character string representation of month (ie. mar, apr, etc.) numerical string (ie. 3, 4, etc.).

according feeble mind following should work:

$('tr:visible .time-col').each(function() {     // convert month string numerical representation     var monthstr = $(this).text().match(/[^\/]*/)[0];     var months = { 'jan': '1', 'feb': '2','mar': '3','apr': '4','may': '5','jun': '6','jul': '7','aug': '8','sep': '9','oct': '10','nov': '11','dec': '12' };     var month = months[monthstr];     $(this).text( $(this).text().replace(monthstr, month) ); }); 

but result replaces proper string 'undefined'. if replace last line:

$(this).text( $(this).text().replace(monthstr, month) ); 

with:

$(this).text(month); 

i correct number (ie. 3, 4, etc.) shown in corresponding table cells.

what gives stack overflow?¿

$(this).text() returns string. modifying string won't touch original.

to modify text, set text of element:

var text = $(this).text(); $(this).text(text.replace(monthstr, month)); 

also, .replace() string first argument replaces first instance of string. you'll have use regex replace of occurrences @ once.


Comments