views:

154

answers:

1

Suppose I have the following HTML:

<div id="test">
  <span style="display:inline;">foo</span>
  <span style="display:none;">bar</span>
  <span style="display:inline;">baz</span>
</div>

.. is there some way in JavaScript for me to get the output "foo baz" (not "foo bar baz")?

$('test').textContent returns the latter, and innerHTML does the equivalent.

I don't care at all if the method used is hackish or roundabout, and can deal with it if it's browser-specific or requires Flash.

However, it must not require anything other than JS or Flash, it must not require any user interaction, and it must not return 'bar'.

Ideas?

+4  A: 

You can do this, but note it won't have a space like your example, because there is no space in the markup:

$("#test :visible").text()

Here's an alternative, like your example spaced out for each span:

var s = new Array();
$("#test :visible").each(function() {
   s.push($(this).text());
});
alert(s.join(' '));
Nick Craver
That is exactly what I wanted. PERFECT.I don't need to do your Array addition, since I can just add a space to the inside of each anchor tag's text.One thing to note: your method requires jQuery.
Sai Emrys
PS Implemented and thanked: http://cssfingerprint.com :)In Safari, this method is 4x faster than my previous one. In Firefox, it's not an improvement. Still worthwhile. Thanks!
Sai Emrys