15

I was trying to pass a string to a JavaScript function.

As it's mentioned here - Pass a string parameter in an onclick function

I'm using this simple code-

<!DOCTYPE html>
<html>

    <body>
        <script>
            name = "Mathew";
            document.write("<button id='button' type='button' onclick='myfunction(\''" + name + "'\')'>click</button>")

            function myfunction(name)
            {
                alert(name);
            }
        </script>
    </body>

</html>

But in the console it's giving an error like Uncaught SyntaxError: Unexpected token }.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Anurag Chakraborty
  • 421
  • 2
  • 5
  • 20
  • Any reason you're trying to add a table cell to a page without a table? – Andy Aug 12 '15 at 09:23
  • Note that the top answer to that question also recommended proper DOM methods, something you might want to consider - it would prevent these kind of issues coming up. – Andy Aug 12 '15 at 09:26

7 Answers7

19

Change your code to

document.write("<td width='74'><button id='button' type='button' onclick='myfunction(\""+ name + "\")'>click</button></td>")
Juned Lanja
  • 1,466
  • 10
  • 21
10

Rename your variable name to myname, bacause name is a generic property of window and is not writable in the same window.

And replace

onclick='myfunction(\''" + name + "'\')'

With

onclick='myfunction(myname)'

Working example:

var myname = "Mathew";
document.write('<button id="button" type="button" onclick="myfunction(myname);">click</button>');
function myfunction(name) {
    alert(name);
}
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
6

The question has been answered, but for your future coding reference you might like to consider this.

In your HTML, add the name as an attribute to the button and remove the onclick reference.

<button id="button" data-name="Mathew" type="button">click</button>

In your JavaScript, grab the button using its ID, assign the function to the button's click event, and use the function to display the button's data-name attribute.

var button = document.getElementById('button');

button.onclick = myfunction;

function myfunction() {
  var name = this.getAttribute('data-name');
  alert(name);
}

DEMO

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andy
  • 61,948
  • 13
  • 68
  • 95
6
document.write(`<td width='74'><button id='button' type='button' onclick='myfunction(\``+ name + `\`)'>click</button></td>`)

Better to use `` than "". This is a more dynamic answer.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
banoth ravinder
  • 1,314
  • 15
  • 18
3

You can pass string parameters to JavaScript functions like below code:

I passed three parameters where the third one is a string parameter.

var btn ="<input type='button' onclick='RoomIsReadyFunc(" + ID + "," + RefId + ",\"" + YourString + "\");'  value='Room is Ready' />";

// Your JavaScript function

function RoomIsReadyFunc(ID, RefId, YourString)
{
    alert(ID);
    alert(RefId);
    alert(YourString);
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MJ X
  • 8,506
  • 12
  • 74
  • 99
2

Try this ...

onclick="myfunction('name')";
Omkar Gharat
  • 99
  • 1
  • 5
1

Use this:

document.write('<td width="74"><button id="button" type="button" onclick="myfunction('" + name + "')">click</button></td>')
Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69
Sameer
  • 705
  • 10
  • 25