Bhupender Bhupender - 1 year ago 46
HTML Question

Combining two or more paragraphs

I am trying out a simple 'point the error in a sentence quiz'. The code seems to work all fine but I would like the three parts of a sentence to appear as in a paragraph.

<p onclick="myFunction()">Being a holiday</p>
<p onclick="myFunction2()"> we decided to go for a picnic</p>
<p onclick="myFunction3()"> to the nearby park.</p>

<p id="demo"></p>

function myFunction() {
document.getElementById("demo").innerHTML = "<img src='../images/right.jpg'>";
function myFunction2() {
document.getElementById("demo").innerHTML = "<img src='../images/wrong.jpg'>";
function myFunction3() {
document.getElementById("demo").innerHTML = "<img src='../images/wrong.jpg'>";

Answer Source

You can put each sentence piece in individual <span> tags instead of <p> tags, and then set the onclick attribute on those <span> tags. Then just put all those in a single <p> tag:

  <span onclick="myFunction()">Being a holiday</span>
  <span onclick="myFunction2()"> we decided to go for a picnic</span>
  <span onclick="myFunction3()"> to the nearby park.</span>

Check out this working fiddle: