I want to get the text that contains the element inside a td. Even not knowing what’s inside this td using the jQuery mytd.text() returns the text successfully in IE8.
However I noticed that this function text() may be not cross browser since it returns me diferent values in Chrome and FF than in IE8. In both Chrome and FF the string result is much longer than in IE8. It’s preceded and appended with empty values " "
As of I have a variable mytd that has the td object how could I retrieve the text that contains in a cross browser manner?
// Does not work
var text = mytd.text()
thank you.
It is cross-browser, but different browsers handle white space in text node differently, and jQuery doesn’t abstract that.
It does provide the
$.trimmethod to help you out though.To strip the white space when you retrieve the text, just do this:
… this assumes
mytdrefers to an existing jQuery variable of course. You could do this:EDIT:
Sorry, I tested that in Safari which implements the native String.trim() method; to have it work in all browsers you should do this: