-3

I'm new to nodes and I'm just trying to wrap my head around them. For whatever reason this isn't working, I've tried anything i could find and no luck.

this is what I'm trying to do "Using the JavaScript function getElementById(), access the text of mySpecialParagraph using nodeValue. Create a paragraph element using createElement() and, using createTextNode(), append a string to this new element that reads “ See: I can use JavaScript”. Using insertBefore(), insert this text before mySpecialParagraph."

<script type="text/JavaScript">
  var msp = document.getElementById("mySpecialParagraph");
  var parent = msp.parent();

  var para = document.createElement("p");
  var textNode = document.createTextNode(" See: I can use JavaScript");
  para.appendChild(textNode);
  parent.insertBefore(para, msp);
</script>
james
  • 101
  • 1
  • 8
  • Are there any errors in the console? – Moose Mar 14 '17 at 20:28
  • no errors, I've put it through a validator and nothing comes up on the web page – james Mar 14 '17 at 20:29
  • Can you please add this to a jsfiddle or something similar and post the link? also, are you trying to use jQuery? Also, please elaborate your question a bit so that we know exactly what you are trying to accomplish (your intention) – Moose Mar 14 '17 at 20:30
  • `var parent = msp.parent();` should, I think, be`var parent = msp.parentNode;` if that's so, however, you should be getting errors reported in the console. – David Thomas Mar 14 '17 at 20:31
  • Please read [ask], and look into how to create a [mcve]. – Heretic Monkey Mar 14 '17 at 20:32
  • https://jsfiddle.net/#&togetherjs=7rmvadgLUT theres the jsfiddle – james Mar 14 '17 at 20:45

1 Answers1

2

.parent() does not exist in plain JS. It should be .parentElement or .parentNode. The latter is the most popular I think. The difference between the two is discussed here.

var msp = document.getElementById("mySpecialParagraph");
var parent = msp.parentNode;

var para = document.createElement("p");
var textNode = document.createTextNode(" See: I can use JavaScript");
para.appendChild(textNode);
parent.insertBefore(para, msp);
<p id="mySpecialParagraph"></p>
Community
  • 1
  • 1
Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239
  • You can see it working right here, so it does work. Something else must be wrong in your code. Probably your HTML. – Bram Vanroy Mar 14 '17 at 20:42
  • oh sorry i didn't know you could run it. I just made a jsfiddle could you have a look? https://jsfiddle.net/#&togetherjs=7rmvadgLUT – james Mar 14 '17 at 20:45
  • `.parentNode` does not need parentheses `()`. It's not a function. It's a value. – Bram Vanroy Mar 14 '17 at 20:49