Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery trim function does not work in IE7?

I'm trying to call the jQuery text() function and run it through the trim() function to remove all trailing and leading whitespace. Seems to work great in Firefox, however, does not work in IE7 (refuses to remove a space trailing at the end).

Any ideas?! Maybe a regex solution?

like image 976
YourMomzThaBomb Avatar asked Mar 23 '10 21:03

YourMomzThaBomb


2 Answers

you most probably have forgotten about jquery chainning ...

try this

$('#selector').trim($('#selector').text())

don't be lazy with

$('#selector').text().trim();//this is wrong...

EDIT

or as @Laserson has simplified it even better, with $.trim($(selector).text());

like image 143
Val Avatar answered Sep 27 '22 16:09

Val


So here's the gist of what was going on. I had some text in a span element and after that text there was a hyperlink/image the user could click to remove the text from the line it was on (see code below). However, I had put a   after the span element text (in the hyperlink's text) to put a bit of padding between the span text and the "delete" image. So even though I was accessing the element's text and trimming $.trim($(this).parent().text()); it would still include that space at the end! Once I removed this extra space, things worked fine. Still no idea why $.trim() wouldn't take care of it though?!

<div>
  <span>
    <strong>SomeText</strong>
  </span>
  <a href="javascript:void(0);" onclick="removeMe();">&nbsp;
    <img src="delete.png" width="15" height="15" border="0" name="imgRemove" />
  </a>
</div>
like image 45
YourMomzThaBomb Avatar answered Sep 27 '22 15:09

YourMomzThaBomb