0

I have some HTML appended which has a timer that constantly changes, but I need that timer in another div. Both timers ofcourse must update simultaneosly.

I have picture here below to show you the better example.

enter image description here

What I have tried so far is this:

var time = $('.flashCurrent').html();
$('.thetime').append(time);

That doesnt insert anything from .flashCurrent.

The code in the script that appends this is this code:

elements.current = $("<div class=\"flashCurrent\"></div>").appendTo(elements.control); // Current elapsed time
gloomy.penguin
  • 5,833
  • 6
  • 33
  • 59
Brian
  • 76
  • 1
  • 8

3 Answers3

1

Try this:

$('.theTime').text(time);

Append is generally used to add a new HTML element within the element you have selected.

Edit: I have created a JsFiddle to show how I believe it should be working.

If this still doesn't work then I suspect there is a scoping issue in how the flashCurrent time is being updated, you may want to investigate jQuery.Live if this is the case.

0

You don't need the html for flashControl div. You only need the text inside of flashCurrent

var time = $('.flashCurrent').text();
$('.thetime').text(time);
Venkata Krishna
  • 14,926
  • 5
  • 42
  • 56
  • @Brian are you inserting after the time is updated in `flashCurrent`? Try to debug and see what is the value of time while inserting. – Venkata Krishna Oct 30 '13 at 23:49
  • Im thinking the problem might be that the HTML i want to get "is not there" in my source code. Because its appended. – Brian Oct 30 '13 at 23:49
  • @Brian - if the source doesn't update the time, then of course, it won't get inserted. – Venkata Krishna Oct 30 '13 at 23:53
  • When I check FireBug the time in between
    does increase. But whatever I try to append the text to 'thetime'-div nothing gets inserted, nothing comes up in FireBug.
    – Brian Oct 30 '13 at 23:55
  • If it's appended it will be 'there' but maybe it doesn't exist until some point *after* you are looking for it. A setinterval could keep trying indefinitely or you could run your script last after everything else has loaded – Moob Oct 31 '13 at 00:01
0

You can set an interval to update the value every second (or more frequently to get them more in sync)

var sync = setInterval(function() {
    $('.thetime').text($('.flashCurrent').text());
}, 1000);
Moob
  • 14,420
  • 1
  • 34
  • 47
  • Well, first I need to get the time inserted into
    which is my problem currently.
    – Brian Oct 30 '13 at 23:53
  • That's covered in the code. It sets the text of .thetime to the text of .flashCurrent. – Moob Oct 30 '13 at 23:56
  • I have setup up live, so you can see whats going on. [link](http://www.restless.dk/play/play.php) – Brian Oct 31 '13 at 00:01
  • 1
    The solution @Moob laid out has a typo. Change setinterval to setInterval and it will behave as expected. – ScottD Oct 31 '13 at 01:56
  • Wow, that did it! Thanks! - But shouldnt there be some sort of jquery method that does it live? – Brian Oct 31 '13 at 09:38
  • You could create a method that 'watched' for the change but that would involve the same sort of set up (with an interval). Good browsers (>ie8) can listen for the `DOMCharacterDataModified` event and trigger a function when the textNode's text changes. [The accepted answer on this SO question has details](http://stackoverflow.com/questions/4789342/textnode-addeventlistener). – Moob Oct 31 '13 at 10:01
  • Oh okay I see, Im learning a lot here. Great. Another thing, how would I redirect when the 'sync' variable has the final time?.. – Brian Oct 31 '13 at 21:07
  • @Brian I'm not sure what you're asking. It sounds like you want to redirect the page when playback has completed. The jquery.customYtPlayer you are using already contains an `onEnd` option. I suggest you look at using that. – Moob Oct 31 '13 at 22:22
  • @Moob yes I have seen that, Ive looked a ton into that file, but I dont get how to pull the options and use them, but I will keep looking. Thanks for your help mate – Brian Oct 31 '13 at 22:26
  • @Brian You should really have asked this as a new question but here you go: `$('.youtube').customYtPlayer({onEnd:function(){window.location="somewhere";}});` I've even put it in [a Fiddle](http://jsfiddle.net/eGPWR/) – Moob Oct 31 '13 at 22:39
  • Okay, on some forums people get mad when asking to much. I cant thank you enough, you have been the best help! – Brian Oct 31 '13 at 22:50