hass
hass

Reputation: 13

Java Script works with a single value but not with two

I have a Java Script that stops working when I try to input two values. This is the script that works:

<HEAD> <script type="text/javascript">
function addsuggest(name)
{
document.getElementById("recipients").innerHTML=document.getElementById("recipients").innerHTML+name+".L ";
}

</script> </HEAD>
<a href=javascript:onClick=addsuggest('3') >click here<a/><br>
<span id=recipients  style="color:blue;"> </span>

Nothing fancy. It just adds 3.L in blue color to the end of a line. I want this function to do some other things so I change function addsuggest(name) to function addsuggest(name, id) and onClick=addsuggest('3') toonClick=addsuggest('3', 'John')

These are the symptoms I have:

  1. It won't work. I tried making it a button. Again it won't work.
  2. When I hover over the link it shows javascript:onClick=addsuggest('3' and not the rest. So it's stopped reading it at the comma.
  3. Rest of the page content goes blue as if it's link but not clickable.

This is the simplest thing I've done with Java Script. It's just inputting values and then printing it. So, what's the matter?

Upvotes: 0

Views: 114

Answers (2)

Cam
Cam

Reputation: 709

You need quotes surrounding the href attribute (and should place them around all other attributes), otherwise the single quotes around the function parameters will break the link:

<HEAD> <script type="text/javascript">
function addsuggest(name, id)
{
document.getElementById("recipients").innerHTML=document.getElementById("recipients").innerHTML+name+id+".L ";
}

</script> </HEAD>
<a href="javascript:onClick=addsuggest('3', 'John');" >click here<a/><br>
<span id="recipients"  style="color:blue;"> </span>

Upvotes: 3

user180100
user180100

Reputation:

<a href="#" onclick="javascript:addsuggest('3');">...</a>

and for readability:

function addsuggest(name)
{
    document.getElementById("recipients").innerHTML += name + ".L";
}

Upvotes: 1

Related Questions