Skip to content
Advertisement

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? Thanks!

Advertisement

Answer

got it now.

 this.audioElement = createElement('audio', {className:'audio-asset', src:'test.mp3'});
 this.audioElement.setAttribute('controls',true);
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement