Search code examples
javascriptjquerydomcodemirror

Creating a DOM element with jQuery - does not implement "node"?


I'm currently implementing CodeMirror and I'm trying to use the addLineWidget function.

It's second parameter takes a DOM Node, so I thought to construct it with jQuery like this:

var widget = $('<span class="ct-description-widget">' + descr + '</span>').get();

However, when I pass it to the function, it throws an exception:

TypeError: Value does not implement interface Node.

How can I make it a true Node? Note that I cannot append it anywhere into the DOM!


Solution

  • I dont know codemirror and not sure what you call a true node. But to return the first DOM element matched, you need to use get(0):

    var widget = $('<span class="ct-description-widget">' + descr + '</span>').get(0);
    

    This is equivalent to:

    var widget = $('<span class="ct-description-widget">' + descr + '</span>')[0];