2

I have a checkbox like this:

<li>  <input type="checkbox" name="collection[1]" value="1234"/>test</li>

When I use $(this).text() it should return "test" but it's returning " test" (ie. with multiple spaces at the beginning)

I also checked the innerText property it has value as "test".

Pavan
  • 337
  • 5
  • 23

1 Answers1

2

From the context of your code sample, I assume this is a reference to the li. In which case the reason for the space preceding the text is because there's a space before it in the HTML.

If you want to remove the space either remove it from the HTML:

<li><input type="checkbox" name="collection[1]" value="1234" />test</li>

Or use trim() on the text() value you receive back to remove leading and trailing whitespace:

var text = $(this).text().trim();
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339