"undefined" when using innerHTML in IE 6

Posted on 2009-04-21
Last Modified: 2013-12-08
I have a problem where my the attached JS function gives me this in FF 3: "4,593.75", but gives me this in IE 6: "undefined,undefinedundefinedundefined.75"

I have stepped through the function and it is doing it's job properly, but IE won't show the digits properly.  All I'm trying to do is to insert commas in appropriate places for monetary values.

Has anyone run into this before? Is there a workaround for IE 6?

function toMonetaryString(x) {	//put in commas where needed, allow for decimal point

  var _x=x.toString();

  var _len=_x.length 1;

  var dot=_x.indexOf('.');

  var _ret="";

  var _start = 0;

  if( dot >= 0 ) {

	_start = dot-1;

	_ret = _x.substr(dot,_x.length);


  var _cnt = 0;

  for(var i=_start;i>=0;i--) {

	_ret=_x[i] _ret;

	_cnt  ;

	if(!(_cnt%3)) {

	  _ret="," _ret;

	  _cnt = 0;



  return _ret;


... (USAGE)

	  var additionalPeople = parseInt(selectedHouseholdSize) - 12;

	  var amt = (5743.75   (additionalPeople * 975.00));

	  var roundedAmt = Math.round(amt*100)/100;

	  btoaLimitAmt = toMonetaryString(roundedAmt);


	document.getElementById('btoaIncomeLimit').innerHTML = btoaLimitAmt;

... (HTML)

    Is your household MONTHLY gross income at or below $ <span id="btoaIncomeLimit"></span>&nbsp;

Open in new window

Question by:marklorenz
    LVL 3

    Accepted Solution

    you can try this function... this works fine for me in both ie and ff
    function formatNumber(options)
    	options.number = options.number||'0';
            options.prefix= options.prefix||'';
            options.precision= options.precision||'0';
            options.suffix= options.suffix||'';
    		return "";
    	options.number = parseFloat(options.number).toFixed(options.precision);
    	options.number+= '';
    	var x = options.number.split('.');
    	var x1 = x[0];
    	var x2 = x.length > 1 ? '.' + x[1] : '';
    	var rgx = /(\d+)(\d{3})/;
    	while (rgx.test(x1)) {
    		x1 = x1.replace(rgx, '$1' + ',' + '$2');
    	return options.prefix+x1 + x2+options.suffix;

    Open in new window

    LVL 9

    Expert Comment

    Now see, if those other browsers would just support VBScript, it would be as simple as
    btoaLimitAmt = FormatCurrency(roundedAmt)
    LVL 12

    Author Closing Comment

    Thanks for the fast answer!  You are right - works in IE 6 as well as the always better FF.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    I had to do a bit of research to find the answer to this question so I thought I'd share my results.  Due to our outdated mainframe systems, we need to downgrade IE9 to IE8 in order to stay compatible.  We also needed to downgrade Java.  In order to…
    Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
    HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    26 Experts available now in Live!

    Get 1:1 Help Now