2

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?

0

7 Answers 7

7

got it now.

 this.audioElement = createElement('audio', {className:'audio-asset', src:'test.mp3'});
 this.audioElement.setAttribute('controls',true);
Sign up to request clarification or add additional context in comments.

Comments

4

For that to work you would need a helper function createElement that works somewhat like this:

var createElement = function(type, props) {
    var $e = document.createElement(type);

    for (var prop in props) {
        if(prop === "className") prop = "class";
        $e.setAttribute(prop, props[prop]);
    }

    return $e;
}

Now you can do:

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

Comments

3

The answers above didn't seem to work for me. Here's what I did:

var audioElement;
audioElement = document.createElement('audio');
audioElement.setAttribute('controls', true);
audioElement.setAttribute('class', 'audio');
audioElement.setAttribute('src', 'test.mp3');
audioElement.setAttribute('type', 'audio/mpeg');

then appended audioElement to the body :

document.body.appendChild(audioElement);

1 Comment

works great, easy to automate. Thanks
2

Following on from @megawac and James's answers, I think the cleanest way to do this for multiple attributes on modern browsers is probably using Object.assign:

const colInput = document.createElement('input');
Object.assign(colInput,{
    type: 'number',
    min: 1,
    value: this.config.cols,
    onchange: this.updateSetting
});

Still not all in one function call, but readable and less verbose than some of the older answers here.

1 Comment

It's not the same as setAttribute though, try setting data-attributes and attributes absent form spec to see what I mean
0
var audioElement = document.createElement('audio');

//your own code for setting the attributes you have already set

audioElement.controls=true;

for boolean attributes like multiple and disabled just set them to true.

Comments

-1

I'm familiar with doing this with jQuery. I wouldn't know exactly how to do it without jQuery.

var $el = $('<audio>', {
    class: 'audio',
    src: 'test.mp3',
    type: 'audio/mpeg',
    controls: 'controls goes here'
});
var html = $el[0].outerHTML;

2 Comments

I need to have controls attribute. your codes don't have it. +1 though.
This is jQuery.. do you have an answer with plain JavaScript?
-2

Enable controls

HTML

<button onclick="enableControls()" type="button">Enable controls</button>
<button onclick="disableControls()" type="button">Disable controls</button>
<button onclick="checkControls()" type="button">Check controls status</button>

JS

<script>
myVid=document.getElementById("audio1");
function enableControls()
  { 
  myVid.controls=true;
  myVid.load();
  } 
function disableControls()
  { 
  myVid.controls=false;
  myVid.load();
  } 
function checkControls()
  { 
  alert(myVid.controls);
  } 
</script>

3 Comments

Not sure what this has to do with the question.
@putvande what with the downvote? Op is asking "I am not sure how to create controls attribute in js. Can anyone help me about it? Thanks! "
Controls attribute is an attribute you add to the tag <audio>, so something like <audio controls="controls">.

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.