var jsDomDoc = new JSDOM(`<!doctype html><html><head><title>some doc</title></head>
<body><div id="my-div" /></body></html>`);
var document = jsDomDoc.window.document;
var elem = document.getElementById("my-div");
console.log(elem);
//returns null
When I put the code you’ve given in a file, then add the line
const { JSDOM } = require("jsdom");
above it, and then run node file.js, I get the correct result of
HTMLDivElement {}
You can also see this in action e.g. in RunKit: https://runkit.com/domenicdenicola/5cf0246c5ba4ca001a40d7d7
So this should all be working fine; maybe it is something specific to your setup.
--
You received this message because you are subscribed to the Google Groups "jsdom" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
jsdom+un...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/jsdom/55f505a3-c191-4192-abcf-d550395b6c37%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To view this discussion on the web visit https://groups.google.com/d/msgid/jsdom/BN6PR13MB1651E9C247FFF4688FBB8794DF180%40BN6PR13MB1651.namprd13.prod.outlook.com.
npm run test
test('get an element by its Id', () => {
const { JSDOM } = require("jsdom"); // add this here, instead of Jest's config file, and it works fine.
var jsDomDoc = new JSDOM(`<!doctype html><html><head><title>some doc</title></head>
<body><div id="my-div" /></body></html>`);
var document = jsDomDoc.window.document;
var elem = document.getElementById("my-div");
console.log(elem);
});
When I put the code you’ve given in a file, then add the line
const { JSDOM } = require("jsdom");
above it, and then run node file.js, I get the correct result of
HTMLDivElement {}
You can also see this in action e.g. in RunKit: https://runkit.com/domenicdenicola/5cf0246c5ba4ca001a40d7d7
So this should all be working fine; maybe it is something specific to your setup.
From: js...@googlegroups.com <js...@googlegroups.com> On Behalf Of Jose Bernal
Sent: Thursday, May 30, 2019 14:07
To: jsdom <js...@googlegroups.com>
Subject: Should be simple: getElementById
Hi trying to instantiate a simple document with an id of "my-div" and then call document.getElementById... but it always returns null.
Shouldn't it return the DOM element of my JSDOM document? How can I rewrite this to correctly return the my-div element?
var jsDomDoc = new JSDOM(`<!doctype html><html><head><title>some doc</title></head>
<body><div id="my-div" /></body></html>`);
var document = jsDomDoc.window.document;
var elem = document.getElementById("my-div");
console.log(elem);
//returns null
--
You received this message because you are subscribed to the Google Groups "jsdom" group.
To unsubscribe from this group and stop receiving emails from it, send an email to js...@googlegroups.com.
Not sure! I don’t know much about Jest, myself.
To unsubscribe from this group and stop receiving emails from it, send an email to
jsdom+un...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/jsdom/58bb13b8-edf2-4c2f-a3b1-87a355acfcc0%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to jsdom+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jsdom/58bb13b8-edf2-4c2f-a3b1-87a355acfcc0%40googlegroups.com.