0

I am making a hunger games simulation and this is my code:

console.log(data);
document.getElementById("playarea").innerHTML = data;
console.log(document.getElementById("playarea").innerHTML);

When I log the variable data to the console it gives me the following result,

<img src='img/fight.png'><br />
You grabbed a loaf of bread out of the crate, the girl from 9 comes to you for a fight.
<br />
<button onclick=fight('You grabbed a loaf of bread out of the crate, the girl from 9 
comes to you for a fight.','','',4,1,2)>
Punch in the head
</button><br />
<button onclick=fight('You grabbed a loaf of bread out of the crate, the girl from 9 
comes to you for a fight.','','',4,1,2)>Punch in the chest</button><br />

When I log the innerHTML of playarea I get

<img src="img/fight.png"><br>
You grabbed a loaf of bread out of the crate, the girl from 9 comes to you for a fight.
<br>
<button onclick="fight('You" grabbed="" a="" loaf="" of="" bread="" out="" the=""  
crate,="" girl="" from="" 9="" comes="" to="" you="" for="" 
fight.','','',4,1,2)="">
Punch in the head
</button><br>
<button onclick="fight('You" grabbed="" a="" loaf="" of="" bread="" out="" the="" 
crate,="" girl="" from="" 9="" comes="" to="" you="" for=""    
fight.','','',4,1,2)="">
Punch in the chest
</button><br>

In the onclick it is separating each word and treating them like variables.

How can I make the innerHTML to be the same as data variable?

1
  • It is better to use jQuery and forget quotes problems. $("#playarea").html(data) Commented Jul 31, 2014 at 14:38

1 Answer 1

2

Change (within data)

<button onclick=fight('You grabbed ...','','',4,1,2)>

into

<button onclick="fight('You grabbed ...','','',4,1,2)">

You can only ommit the quotes if the value is just one word.

Sign up to request clarification or add additional context in comments.

2 Comments

+1; this is the answer. To elaborate on this, when you leave out the quotes, the additional words are parsed as attributes of <button>, thus the grabbed="" a="", etc.
Thanks, it works fine now. I had to use both types of quotes inside of a string so i added + ' " ' + inside of the data variable and it works great!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.