CDaug
CDaug

Reputation: 13

Javascript Errors when changing paragraphs

So I'm trying to make a page display different paragraphs for each button pressed. However I keep getting some errors where the button will not run the function correctly. Ex

function examplefunction() {
  document.getElemtentById("paragraphtobechanged").innerHTML = "When I put more "" it fails in other words it will not write this part."
  }
<button onclick="examplefunction()">Change Paragraph</button>
<p id="paragraphtobechanged"></p>

Upvotes: 1

Views: 39

Answers (3)

smik
smik

Reputation: 111

The issue here is with the actual string.

// Option 1
"When I put more \"\" it fails in other words it will not write this part."


// Option 2
'When I put more "" it fails in other words it will not write this part.'

Note: Option 2 works with your case but if you're using both single-quotes and double-quotes within your actual string you'll have to escape one or the other.

Upvotes: 0

Dylan Corriveau
Dylan Corriveau

Reputation: 2557

@TheDude was on the right track, but there's something else. You spelt element wrong in your example:

function examplefunction() {
                V
  document.getElementById("paragraphtobechanged").innerHTML = "When I put more \"\" it fails in other words it will not write this part."
  }

Upvotes: 1

TheDude
TheDude

Reputation: 3952

It should be:

function examplefunction() {
  document.getElemtentById("paragraphtobechanged").innerHTML = "When I put more \"\" it fails in other words it will not write this part."
  }

You need to escape the double quotation marks. Or you can do:

function examplefunction() {
      document.getElemtentById("paragraphtobechanged").innerHTML = 'When I put more "" it fails in other words it will not write this part.'
      }

Upvotes: 2

Related Questions