Member Avatar for Member #578978

Hi,

Can someone please tell me what the correct syntax for this is... I've tried everything.

parent.TopFrame.document.FormName.inputAreaName.value =(tableName.rows[1].cells[0].innerHTML).trim();

It works without the trim() function, as soon as I put it in, it throws an error and stops working.

Thanks,
-Ash

Dani AI

Generated

The complaint you hit is a classic: calling .trim() on the thing you grabbed from the DOM can fail in older JavaScript engines or if what you grabbed is not a plain string. was on the right track with a polyfill for old browsers, and correctly pointed out that you need to trim the string value you read from the cell (not the element itself). See MDN for the language reference and compatibility notes for String.prototype.trim (ES5): String.prototype.trim — MDN.

Recommended, modern approach:

  • Read plain text (not HTML) from the cell using textContent (fallback to innerText for older browsers).
  • Guard against null/undefined before trimming.
  • Convert non-breaking spaces if needed (they may survive a basic trim).

Example pattern:

var cell = someTable.rows[1].cells[0];
var raw = cell.textContent || cell.innerText || '';
var safe = String(raw);
var cleaned = safe.trim().replace(/\u00A0/g, ' ');
myInput.value = cleaned;

If supporting very old browsers, either include a small polyfill (MDN shows a safe polyfill) or use jQuery.trim() if jQuery is present (jQuery.trim — API). Troubleshooting checklist: inspect the console for "trim is not a function" (means no native trim or non-string), confirm the element exists before accessing, and remember cross-frame same-origin rules will block access to parent/other frames (see same-origin policy).

Recommended Answers

All 3 Replies

I think what you want to trim goes inside the brackets

so something like
trim(tableName.rows[1].cells....)

Javascript strings don't have a trim function, to add one you can do something like

String.prototype.trim = function ()
{
  return this.replace(/^\s+|\s+$/, '');
};

somevar = "I have a string with trailing spaces       ";
somevar_trimmed = somevar.trim(); // "I have a string with trailing spaces"

// So yours will be
parent.TopFrame.document.FormName.inputAreaName.value = tableName.rows[1].cells[0].innerHTML.trim();

Quite right, sorry had my PHP head on

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.