2

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!

3
  • @billyonecan oh, that did it. I should have seen the square brackets in firebug :) Commented May 28, 2013 at 8:37
  • 2
    @billyonecan: Did you meant .get(0)?? Commented May 28, 2013 at 8:37
  • @PalashMondal .get()[0] give same result as .get(0) of course but the usual syntax is to pass index as param Commented May 28, 2013 at 8:42

1 Answer 1

5

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];
Sign up to request clarification or add additional context in comments.

2 Comments

Not totally really, .get provides negative indexes feature, but that's the right answer. Great!
@LightStyle nice point! Ya, get() let us to use negative index to start from the end.

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.