0

I have an element variable:

element = '<div class="c-element js-element">Something Here</div>';

Needed to select this element like:

$(element) 

it returns no string return like:

n.fn.init [div.c-element.js-element, context: div.c-element.js-element] // and more

How can I select a given string element in javascript dom like jQuery selector?

6
  • There aren't jQuery selectors (for the most part), those are CSS selectors (look up querySelector) Commented Aug 1, 2018 at 7:33
  • Do you which to create the element manually, or just get an element from the dom using document.querySelector? Commented Aug 1, 2018 at 7:33
  • Possible duplicate of jQuery, get html of a whole element Commented Aug 1, 2018 at 7:35
  • there is given element fully don't have class name or id, it is using inside a function. document.querySelector(element): Uncaught DOMException: Failed to execute 'querySelector' on 'Document': '[object HTMLDivElement]' is not a valid selector Commented Aug 1, 2018 at 7:38
  • @NitinSawant i need to use javascript dom, without jquery Commented Aug 1, 2018 at 7:39

1 Answer 1

1

$(element).get(0) will return the DOM element. Also you could use native bindings:

const element = document.createElement('div');
element.classList.add('c-element', 'js-element');
element.innerHTML = 'Something here'
Sign up to request clarification or add additional context in comments.

Comments

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.