Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create element with attribute in js?

I am trying to create a audio tag in JavaScript.

I have the following

this.audioElement = createElement('audio', {className:'audio', src:'test.mp3', type:'audio/mpeg'});

The audio tag I want to appear in HTML

<audio controls src='test.mp3' type='audio/mpeg'></audio>

I am not sure how to create controls attribute in js. Can anyone help me about it?

like image 767
FlyingCat Avatar asked Mar 20 '26 17:03

FlyingCat


1 Answers

got it now.

 this.audioElement = createElement('audio', {className:'audio-asset', src:'test.mp3'});
 this.audioElement.setAttribute('controls',true);
like image 72
FlyingCat Avatar answered Mar 22 '26 07:03

FlyingCat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!