-3

I have ran:

let x = document.createElement('iframe');

Result:

<iframe>
</iframe>

-- And

x.src = "https://www.google.com";

Then the result was:

<iframe src="https://www.google.com">
</iframe>

The code for the element to append to:

<p>Test</p>

And everything was fine until I tried to append (

document.querySelector('prg > p').appendChild(x);

): It just said:

Uncaught TypeError: Cannot read property 'appendChild' of null
at <anonymous>:1:36

How can I fix this or set the value to not be null in the code?

Note: I have tried using DIV and SPAN but to no luck.

DCT A
  • 13
  • 4
  • What do you expect from `document.querySelector('prg > p')`? There is no `prg` in your code. There are also neither `'div'` nor `'span'` in your code. – Thomas Sablik Feb 03 '21 at 20:21
  • Does this answer your question? [Why does jQuery or a DOM method such as getElementById not find the element?](https://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element) – John Montgomery Feb 03 '21 at 20:21
  • _"How can I fix this or set the value to not be null in the code?"_ Select an existing element. – Thomas Sablik Feb 03 '21 at 20:27

1 Answers1

0

This is because querySelector is not correct and there is not any tag element found, because there is not any prg tag in html. so if prg is class then add dot in front of that or if it is an id then add # sign so then it's work fine.

and you can check it is null or any tag found like this

document.querySelector('prg > p') ? document.querySelector('prg > p').appendChild(x) : '' ;
Muhammad Usman
  • 209
  • 1
  • 12