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?

Alex Ananjev
  • 269
  • 3
  • 16
devugur
  • 1,339
  • 1
  • 19
  • 25
  • There aren't jQuery selectors (for the most part), those are *CSS* selectors (look up `querySelector`) – CertainPerformance Aug 01 '18 at 07:33
  • Do you which to create the element manually, or just get an element from the dom using `document.querySelector`? – Icepickle Aug 01 '18 at 07:33
  • Possible duplicate of [jQuery, get html of a whole element](https://stackoverflow.com/questions/3614212/jquery-get-html-of-a-whole-element) – Nitin Sawant Aug 01 '18 at 07: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` – devugur Aug 01 '18 at 07:38
  • @NitinSawant i need to use javascript dom, without jquery – devugur Aug 01 '18 at 07:39
  • Please elaborate on *"I need to use javascript dom, without jquery"* - your question is tagged [jquery] and you have used jquery in the question. – freedomn-m Aug 01 '18 at 08:08

1 Answers1

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'