1

In the following code, I'm working on a simple timer in JS. The problem is that the resulting code only shows one set of divider colons.

var divider = $('<span>').addClass('divider').text(':');
stopwatchFace = stopwatchFace.append(timeHour).append(divider)
                             .append(timeMin).append(divider).append(timeSec);

Is there a reason why the first one isn't being picked up? Should I be explicitly defining a divider1 and a divider2 object?

1
  • You don't need to use chaining in this case. jQuery.fn.append() appends all parameters that it receives.. so you can use something like: stopwatchFace.append(timeHour, divider, timeMin, divider.clone(),timeSec); Commented Apr 11, 2013 at 19:51

2 Answers 2

4

If you append an already-appended element, the result is that it is moved.

You need to clone the element. In vanilla JS, this would be as simple as divider.cloneNode(true).
In jQuery it's simple too: divider.clone(). Thanks Boaz for the info ^_^

Sign up to request clarification or add additional context in comments.

2 Comments

The jQuery equivalent is the .clone() method.
@Kolink I have quite a strange question what is vanilla JS ? :)))
2

Use need to clone divider. as divider is a dom single element cant exist two place at same time.

var divider = $('<span>').addClass('divider').text(':');
stopwatchFace = stopwatchFace.append(timeHour).append(divider.clone())
                             .append(timeMin).append(divider).append(timeSec);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.