views:

78

answers:

2

I have website that converts Japanese Kanji into romaji (roman letters):

and the output shows and hides with CSS what the user needs to see depending on their input criteria. For example:

<div id="output"><span class="roman">watashi</span> <span class="english">I</span></div>

The interface allows the user to flip between and output of watashi or I depending on what they want to see. The CSS hides one or the other using JQuery and a toggle button. (the hiding mechanism involves simple adding a class to the body and letting CSS do its thing).

The problem is that when users copy/paste the text into Word it copies everything. So I decided to use a system to copy paste the text using Javascript and JQuery, but the problem repeats itself:

$('#output').text() outputs watashi I even if I is invisible on the page itself rather than watashi. Is there any way to get just the visible text?

+3  A: 

Use the :visible selector of Jquery

In your case I think you want to do:

$('#output').children(":visible").text()
marcgg
btw, according to OP question, only one of #output's child is hidden, is that doable with that way? When I test `$('#output:visible').text()` its still showing "Watashi I", but OP wants only "Watashi", isn't it?
S.Mark
@s.mark: You must be right. I edited my answer, I think it should work. If not try *:visible or something like that. You could also test .css("display")!="none"
marcgg
I think this is the wrong approach because it consists of re-implementing already existing browser functionality (copy/paste).
Kaze no Koe
@marcgg, cool! this one working when the display is none, +1
S.Mark
@smark: good! It's clearly not the best solution. The best one would be to only load into the page the language wanted, but this will fix the OP's problem right away
marcgg
hey Marcgg, this seems to work almost perfectly. It does indeed copy the visible text but if I change the visible text via the 'rotate' button then a copy action still only gets the original visible text - not the updated one (this is easier to see if you go to the actual site and click the rotate icon to visualise how the text changes). So I guess it becomes a bit more complicated if the visible text changes. (btw, I wanted to 'up' your suggestion but I'm new here and it won't allow me!)
Gazzer
I got this to work. The above issue was connected to something else.
Gazzer
A: 

Instead of hiding a span, remove the span element and keep a reference to it. When the user clicks on the toggle button, remove the other one and insert the one you kept a reference to. The user won't be able to select something that isn't in the DOM anymore.

Kaze no Koe
hey 'voice of the wind' :-) I'd prefer to find a solution that doesn't involve removing elements at the moment as that would require a major rewrite of the coed.
Gazzer
Kaze no Koe