How would I add currentTime on to The 1st code

Both of these codes work.

How would I add currentTime to The 1st code.

1.)

onclick="document.getElementById('player4').volume=1.0;
player4.paused ? player4.play() :player4.pause()

2.)

onclick="document.getElementById('player4').currentTime=80;
player4.paused ? player4.play() :player4.pause()

Have you tried just adding it as another statement?

Like this:

onclick="document.getElementById('player4').currentTime=80;document.getElementById('player4').volume=1.0;
player4.paused ? player4.play() :player4.pause()

The semicolon is a delimiter between statements. There is no limit to the amount of code you can include in that event call (although it is really bad practice to go too far).

1 Like

Why is it a bad practice? After all, the code still works and runs amazingly fast! :wink:

3 Likes

It is neater (more DRY) to call JS files rather than cramming a lot of code in the HTML file (Separation of Concerns).
Aren’t the current Best Practices in JS to register event handlers outside these inline tags?

2 Likes

I only wanted to use 1 getElementById, not 2 of them.

Haven’t you assigned it to that player4 variable anyway? As in

player4.play()

etc.

Ahh, in that case you can use Object.assign

Object.assign(document.getElementById('player4'), {
    currentTime:80,
    volume: 1.0
});

Or as @m3g4p0p has mentioned just player4, but that’s normally a bad practice to rely on without first assigning it to a variable.

var player4 = document.getElementById('player4');
Object.assign(player4, {
    currentTime:80,
    volume: 1.0
});

I suspect though that you’ll be instead doing something like this:

Object.assign(player4,{currentTime:80,volume:1.0});
1 Like

Yes they are. @asasass is a person that has been resistant against all efforts to improve. Instead he wants to employ the fewest characters, and insists on inlining everything for reasons of pure execution speed.

@asasass - Please correct me if I have misunderstood these characteristics about you in any way.

1 Like

Why? If you are not going to do it properly in a separate file then using multiple getElementById calls is the simplest way to code it when you are jumbling it with your HTML anyway.

That was not specified (or event hinted) in your original [quite brief] post.

1 Like

English may not be his first language. What he wanted can be interpreted from “How would I add currentTime to The 1st code.” but can be expressed much more clearly, as indeed occurred later.

He was asked that elsewhere and says that it is. He’s also been pointed to resources on how to ask clear questions, so I’m not sure how else we can help in that respect.

Thank you.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.