-8

I'm having some trouble passing multiple parameters to a Javascript function.

The function:

function selectDino(dinosaurName, dinosaurHealth, dinosaurTraits)
{
    localStorage.setItem("dinoName", dinosaurName);
    localStorage.setItem("dinoHealth", dinosaurHealth);
    localStorage.setItem("dinoTraits", dinosaurTraits);
    location.replace("nextpage.html");
}

....

The call (mainly the "selectDino()" bits):

<img class="leftDino" src="dino1.png" onmouseover = getDino("dinoOne") onclick=selectDino("dinoOne", "20", "Banterous")>
<img class="centerDino" src="dino2.png" onmouseover = getDino("dinoTwo") onclick=selectDino("dinoTwo", "18", "Spiky")>
<img class="rightDino" src="dino3.png" onmouseover = getDino("dinoThree") onclick=selectDino("dinoThree", "22", "Bitey")>

Whenever I look on the Chrome Developer Console, it tells me the following:

Uncaught SyntaxError: Unexpected token }

Can anyone help with this?

EDIT

Added single quotes around the attributes and that's fixed the problem.

String
  • 155
  • 1
  • 5
  • 12

2 Answers2

8

onclick and onmouseover functions needs " " like that onclick="foo();"

<img class="rightDino" src="dino3.png" onmouseover="getDino('dinoThree')" onclick="selectDino('dinoThree', '22', 'Bitey')">
Özgür Ersil
  • 6,909
  • 3
  • 19
  • 29
3

Wrap your HTML attributes in quotes, and close your HTML elements (/>):

<img class="leftDino" src="dino1.png" onmouseover="getDino('dinoOne')" onclick="selectDino('dinoOne', '20', 'Banterous')" />
<img class="centerDino" src="dino2.png" onmouseover="getDino('dinoTwo')" onclick="selectDino('dinoTwo', '18', 'Spiky')" />
<img class="rightDino" src="dino3.png" onmouseover="getDino('dinoThree')" onclick="selectDino('dinoThree', '22', 'Bitey')" />

Keep in mind I also changed the inner quotes to single quotes ('), otherwise, you'd break your HTML entirely.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • In HTML5, self-closing elements : `area, base, br, col, command, embed, hr, img, input, keygen, link, meta, param, source, track, wbr` don't need `/` at the end – Magicprog.fr Jul 02 '15 at 14:25
  • 2
    @Magicprog.fr: Sure, but it's [still a good idea](http://stackoverflow.com/questions/3558119/are-self-closing-tags-valid-in-html5) to close your tags. – Cerbrus Jul 02 '15 at 14:26