I am trying to create waveform using wavesurfer.js for dynamically created elements using JavaScript. Here is my code:
$(document).ready(function() {
var id = 9;
$("#audio").append('<div class="row" id="wave' + id + '"></div>');
$('#audio').on('DOMNodeInserted', 'div', function() {
var selectedId = $(this).prop('id');
console.log(selectedId);
window['waveForm' + selectedId] = Object.create(WaveSurfer);
window['waveForm' + selectedId].init({
container: $(this),
waveColor: 'violet',
progressColor: 'purple'
});
window['waveForm' + selectedId].on('ready', function() {
window['waveForm' + selectedId].play();
});
window['waveForm' + selectedId].load(selectedId + '.mp3');
});
});
console.log displays the correct id of the element, so the selector seems to work, however wavesurfer.js gives the following error:
Uncaught TypeError: this.container.appendChild is not a function
Sorry, I was at work on break before and couldn't get into it that deep.But try not to open duplicate questions it is better to update the question as we work through the issue (unless of course a totally different problem emerges)
The below is a functional fully commented implementation of what you are trying to do with a bit simpler of an approach.
Here's a copy hosted on my server to avoid the cross domain issues
jQuery
Html
From the github of
wavesurfer.js
, thecontainer
parameter in theinit
function is described as following:It doesn't mention whether or not a jQuery object is accepted, so try replacing
$(this)
withthis
: