This is what worked for me and the step by step discovery:
The output is from chrome console
First locato the parent node containing the nasty whitespace
$('.controls label[class="radio"]').parent();
[<div class="controls">
<label class="radio">…</label>
" "
" "
<label class="radio">…</label>
" "
" "
</div>]
You can see this is wrapped in an array from the [] brackets jQuery will always return an array like structure even when a single item has been found.
So to get to the HTMLElement we take the first item in the array at index 0
$('.controls label[class="radio"]').parent()[0];
<div class="controls">
<label class="radio">…</label>
" "
" "
<label class="radio">…</label>
" "
" "
</div>
Note how there are no more [] brackets. The reason we need to do this is because jQuery will ignore whitespace in the dom but HTMLElement won't, look what happens when we access the childNodes property
$('.controls label[class="radio"]').parent()[0].childNodes;
[<label class="radio">…</label>,
" ",
" ",
<label class="radio">…</label>,
" ",
" "]
We have an array again, yes you spot the [] brackets but do you see another difference, look at all the commas, which we couldn't get with jQuery.
Thank you HTMLElement but now we can go back to jQuery because I want to use each instead of a for loop, do you agree with me?
So lets wrap the array in jQuery and see what happens:
$($('.controls label[class="radio"]').parent()[0].childNodes);
[<label class="radio">…</label>,
" ",
" ",
<label class="radio">…</label>,
" ",
" "]
Perfect! we have exactly the same structure still but nnow inside a jQuery object so lets call each and print "this" to console to see what we have.
$($('.controls label[class="radio"]').parent()[0].childNodes).each(function () {
console.log('|'+$(this).html()+'|');
});
|<input id="gender_f" name="gender" type="radio" value="f">Female|
|undefined|
|undefined|
|<input id="gender_m" name="gender" type="radio" value="m" checked="">Male|
|undefined|
|undefined|
So we use jQuery to get the html of each element, standard stuff `$(this).html and because we can't see white space lets pad it with a pipe |, good plan but what do we have here?
As you can see jQuery is not able to turn the whitespace to html and now we have undefined. But this is even better because where a space might be truthy undefined is definitely falsy =)
So lets get rid of the suckers with jQuery. All we need is $(this).html() || $(this).remove();
lets see:
$($('.controls label[class="radio"]').parent()[0].childNodes).each(function () {
$(this).html() || $(this).remove();
});
[<label class="radio">…</label>,
" ",
" ",
<label class="radio">…</label>,
" ",
" "]
Oh dear.. but don't fear! Each still returns the previous structure not the one we've changed, lets look at what our initial query returns now.
$('.controls label[class="radio"]').parent();
[<div class="controls">
<label class="radio">…</label>
<label class="radio">…</label>
</div>]
And Wallah! All sexy and pretty =)
So there you have it, how to remove whitespace between elements/tags ala jQuery style.
nJoy!